1

I have an array of ids ['id1', 'id3'].

I also have an array of items:

[
      {
        children: [{
                  children: [{
                            id: "id1", //This is value I need to find
                            status: { state: false}, //this is value I need to change
                            }],
                  }],
      },
      {
        children: [{
                  children: [{
                            id: "id2", 
                            status: { state: false}, 
                            }],
                  }],
      },
      {
        children: [{
                  children: [{
                            id: "id3", 
                            status: { state: false}, 
                            }],
                  }],
      },
    ]

My goal is to find every item by id from first array, and change attribute state, then return all items having included those I have changed.

This was my try, but it returns all items again, also Im not sure how to change the attribute.

items.filter(item =>
  item.children.map(child =>
     child.children.map(object =>
        idsArray.map(id => id === object.id)
)))
6
  • May be it will be helpful to update the question with some example data which can be used to execute the logic - as well as share your current logic (with map function, containing more map functions inside) to help community members refine/update that to make it work? Commented May 25, 2022 at 8:05
  • 1
    First of all, your data ins invalid ... both, the outer and the inner children are arrays, so their elements should either be array, objects or primitive types, but [ children: [...]] or [ id: "id1" ...] are neither. Second: Do you want to update the existing array or do you want to create a new array? Commented May 25, 2022 at 8:24
  • you Json file is invalid. try correcting it first. also if you can share the code which you wrote for above Commented May 25, 2022 at 8:27
  • "but it returns all items again". So what do you want to be returned? Please post the expected result based on your data ... Commented May 25, 2022 at 8:30
  • Sorry for invalid data example in first version of question. Im new at this and hope next time I will be able to write my question right. Thank you for all your advices, for future. Commented May 25, 2022 at 8:33

1 Answer 1

1

I think you can use recursive function something like below :

let ids = ["id1", "id2"];
let arrayOfItems = [
  {
    children: [
      {
        children: [
          {
            id: "id1",
            status: {
              state: false
            }
          }
        ]
      }
    ]
  },
  {
    children: [
      {
        children: [
          {
            id: "id2",
            status: {
              state: false
            }
          }
        ]
      }
    ]
  },
  {
    children: [
      {
        children: [
          {
            id: "id3",
            status: {
              state: false
            }
          }
        ]
      }
    ]
  }
];

function changeStatus(arrayOfItems, ids) {
  return arrayOfItems.map((e) => {
    if (e.id && ids.includes(e.id)) {
      return { ...e, status: { state: true } };
    } else if (e.children) {
      return { ...e, children: changeStatus(e.children, ids) };
    } else {
      return { ...e };
    }
  });
}

console.log(changeStatus(arrayOfItems,ids));

Sign up to request clarification or add additional context in comments.

1 Comment

As we currently neither know how the input data looks like (what OP has posted so far is just invalid) nor what the output should look like (OP hasn't said anything about that) this is mere speculation ...

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.