0

Need better solution to extract data from nested objects. I'm currently working with an object that is a nested array.

let data = [
    {
        name: "Var 1",
        id: "1",
        child: [
            {
                name: "Var 2",
                id: "2",
                child: [
                    {
                        name: "Var 3",
                        id: "3",
                    },
                    {
                        name: "Var 4",
                        id: "4",
                    },
                ],
            },
{
                name: "Var 5",
                id: "5",
                child: [
                    {
                        name: "Var 6",
                        id: "6",
                    },
                    {
                        name: "Var 7",
                        id: "7",
                    },
                ],
            },
        ],
    },
];

what i have for searching this data is just id from last child, e.g:

let knownId = 3

and i`m expect the outcome is just like this

[
    {
        name: "Var 1",
        id: "1",
        child: [
            { name: "Var 2", id: "2", child: [{ name: "Var 3", id: "3" }] },
        ],
    },
];

im using lodash for simple silly solution like this

let first: any = _.filter(data, (i1: any) =>
    _.some(i1.child, (i2) => _.some(i2.child, (i3) => i3.id == knownId ))
);

let secondData: any = _.cloneDeep(_.first(first));

let second: any = _.filter(secondData.child, (i1: any) =>
    _.some(i1.child, (i2) => i2.id == knownId )
);

I got the expected result after some map and join, but I know it's not optimal. So I'm going to try another solution that uses a function like this.

function deep(arr: any, id: any) {
  _.some(arr, function (val) {
    if (val.id == id) return true;
    if (_.isArray(val.child)) {
      deep(val.child, id);
    }
  });
}

But when I call this function from within the filter method, it simply does not work and returns undefined. I require a better solution to my problem in order to improve my data filtering performance. Please kindly help me. thanks :)

4
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Sep 27, 2022 at 6:57
  • Does this answer your question? stackoverflow.com/questions/43248073/… Commented Sep 27, 2022 at 7:24
  • @marcobiedermann yes that work, but its just work like my solution. i can`t reproduce the code. thanks btw :) Commented Sep 27, 2022 at 9:17
  • i notice that the problem of my deep function not working is because i'm not returning the funtion. Commented Sep 27, 2022 at 9:29

1 Answer 1

2

I guess your filter could be like this

function hasDeep(arr, id) {
  return arr.some(function (val) {
    if (val.id == id) return true;
    if (Array.isArray(val.child) && hasDeep(val.child, id)) return true;
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

this work perfectly, thanks :). I just add some condition to handle if arr has value undefined and its done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.