Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Map through array of arrays, to find id and change object

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)
)))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!