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

175
Views
Find item if exist in deep nested children array?

I need to find if item exist in deep nested array.

Example :

let arr = [
  {
   id: 1 ,
   title : 'Test' ,
   children: 
  [
    {id: 1 , title: 'Title' , hasChild : true },
    {id: 1 , title: 'Title' , hasChild : false },
    {id: 1 , title: 'Title' },
  ] 
 },
]

I need to looping thought arr.children and trying to find if it has a property with name hasChild find item and set it on false.

After looping I need to get an array like :

let arr = [
  {
   id: 1 ,
   title : 'Test' ,
   children: 
  [
    {id: 1 , title: 'Title' , hasChild : false }, //here is changed hasChild to false
    {id: 1 , title: 'Title' , hasChild : false },
    {id: 1 , title: 'Title' },
  ] 
 },
]

I am find way but i need better solution :

    arr.map(it) => {
      if (it.hasChild) {
        it.dashCheck = false; 
      }
    });
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Demo :

let arr = [{
  id: 1 ,
  title : 'Test' ,
  children: 
  [
    {id: 1 , title: 'Title' , hasChild : true },
    {id: 1 , title: 'Title' , hasChild : false },
    {id: 1 , title: 'Title' },
  ] 
}];

arr.forEach((obj) => {
    obj.children.forEach((childObj) => {
    if (childObj.hasChild) {
        childObj.hasChild = false;
    }
  });
});

console.log(arr);

about 4 years ago · Juan Pablo Isaza Report

0

You can use recursive functions to make operations on inifitely nested objects/arrays etc.

function recursiveUpdate(array, index) {
  const element = array[index];
  if(!element) return;
  if(Object.keys(element).indexOf('children') > -1) {
    element.hasChild = true;
    recursiveUpdate(element['children'], 0);
  } else {
    element.hasChild = false;
    recursiveUpdate(array, index+1);
  }
}

recursiveUpdate(arr, 0);
about 4 years ago · Juan Pablo Isaza Report

0

Edit: Seems like the question was altered after I gave my answer. Updated my answer to reflect this.

If the structure of arr is always like the one you mentioned in your question, you could simple iterate over the children by using the forEach method of the Array class:

arr.forEach(item => {
  item.children.forEach(child => {
    // Edit: Revised solution 
    if (child.hasChild === true) {
      child.hasChild = false;
    }
    // Edit: Previous solution
    // child.hasChild = child.hasChild === true ? false : child.hasChild;
  });
});

Note: This will alter the original array and not create a copy with the altered value for hasChild.

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!