Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find array with children javascript

const find = (array = [], id) => {
  for (const item of array) {
    const result = item.id === id ? item : find(item.children, id);
    if(result) return result;
  }
};

const commentList = [{ id: 1, text: 'A', children: [{ id: 2, text: 'B' }] }, { id: 4, text: 'asd', children: [] }, { id: 5, text: 'vx', children: [{ id: 7, text: 'xxss' }] }, { id: 8, text: 'ghfdh', children: [{ id: 15, text: 'I want to take this' }] }, { id: 10, text: 'A', children: [{ id: 18, text: 'Bsda' }] }];

const result = find(commentList, 15);

console.log(result);
Comment

PREVIOUS NEXT
Code Example
Javascript :: Convert number to array of digits js 
Javascript :: sequelize order by 
Javascript :: javascript console log execution time 
Javascript :: get hours and minutes and seconds from date in javascript 
Javascript :: check undefined in javascript 
Javascript :: next js absolute path 
Javascript :: hex to rgba in js 
Javascript :: JS retrieve a String’s size 
Javascript :: Binary Agents 
Javascript :: vue watch props 
Javascript :: javascript array group by 
Javascript :: add image in react native 
Javascript :: daysinmonth javascript 
Javascript :: javascript trim 
Javascript :: window scroll down javascript 
Javascript :: hello word in js 
Javascript :: jquery check if has class 
Javascript :: altv rpc 
Javascript :: textField input font color React Material UI 
Javascript :: how to edit website in browser using javascript on google chrome 
Javascript :: js onscroll event 
Javascript :: validate latitude longitude javascript 
Javascript :: jquery set radio button value 
Javascript :: javascript get current date 
Javascript :: two digit js' 
Javascript :: to add autofix when saving files in Eslint 
Javascript :: node js cron example 
Javascript :: javascript assign 
Javascript :: get placeholder innertext 
Javascript :: unload in jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =