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

421
Views
How to find just first child element in array? Javascript

I need to find first child element in array and just return it. Here is problem because here is few children elements and i need loop thought each and return every first children element.

Example of array:

let allItems = 
[{  id: 1 , 
    name: 'Test 1' ,
      children: [
       id: 12,
       title: 'Child element'
    ]
},
{  
    id: 2 , 
    name: 'Test 2' 
}, 
{  
    id: 3 , 
    name: 'Test 3',
    children: [
    id: 12,
    title: 'Child element',
       children: [
         id: 123,
         title: 'GRAND Child element',
      ]
    ]
}]

What's the problem here? Since there can be many children elements, do I need to find a parent for each of those elements?

After looping i need array to be:

[{  id: 1 , 
    name: 'Test 1'
},
{  
    id: 2 , 
    name: 'Test 2' 
}, 
{  
    id: 3 , 
    name: 'Test 3'
}]

Wihout children elements.

What I am try:

allItems.map(item => item).filter(filteredItem => !filteredItem.children);

But this is no return me good results

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Based on your expected output, here is my solution.

Also note, that you had missing curly braces with your children.

See you modified snippet below:

let allItems = [{
    id: 1,
    name: 'Test 1',
    children: [{
      id: 12,
      title: 'Child element'
    }]
  },
  {
    id: 2,
    name: 'Test 2'
  },
  {
    id: 3,
    name: 'Test 3',
    children: [{
      id: 12,
      title: 'Child element',
      children: [{
        id: 123,
        title: 'GRAND Child element',
      }]
    }]
  }
]

console.log(allItems.map(item => {
  return {
    id: item.id,
    name: item.name
  }
}))

about 4 years ago · Juan Pablo Isaza Report

0

Using map and destructuring is a nice way to achieve what you're looking for

let allItems = [{
    id: 1,
    name: 'Test 1',
    children: [{
      id: 12,
      title: 'Child element'
    }]
  },
  {
    id: 2,
    name: 'Test 2'
  },
  {
    id: 3,
    name: 'Test 3',
    children: [{
      id: 12,
      title: 'Child element',
      children: [{
        id: 123,
        title: 'GRAND Child element',
      }]
    }]
  }
];

const res = allItems.map(x =>  {
  const {id, name} = x;
  return {id, name};
});
console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Use these propertys for call first child: .firstchild==>this property calls first Node .firstElementChild==>calls first element

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!