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

203
Views
Trying to find an object with a key in a nested array in javascript

I'm trying to pass a key to a recursive function and find and return that object with that key.

This is the array that I like to search into

const treeData = [
  {
    title: 'parent 1',
    key: '0-0',
    children: [
      {
        title: 'parent 1-0',
        key: '0-0-0',
        disabled: true,
        children: [
          {
            title: 'leafffff',
            key: '0-0-0-0',
            disableCheckbox: true,
          },
          {
            title: 'leaf',
            key: '0-0-0-1',
          },
        ],
      },
    ],
  },
];

And this is my current function

function findNode(x, key) {
    if (x.key === key)
        return x
    else if (Array.isArray(x)) {
        return x.map(item => {
            const r = findNode(item, key)
            if (r) return r
            if (item.children)
                return findNode(item.children, key)
        })
    }
}

Live example:

const treeData = [ { title: 'parent 1', key: '0-0', children: [ { title: 'parent 1-0', key: '0-0-0', disabled: true, children: [ { title: 'leafffff', key: '0-0-0-0', disableCheckbox: true, }, { title: 'leaf', key: '0-0-0-1', }, ], }, ], }, ];
function findNode(x, key) {
  if (x.key === key)
    return x
  else if (Array.isArray(x)) {
    return x.map(item => {
      const r = findNode(item, key)
      if (r) return r
      if (item.children)
        return findNode(item.children, key)
    })
  }
}

const node = findNode(treeData, '0-0-0-1')
console.log(node)

Using this function I can find the result but is not in the current format. I like to get a single object but gives me nested array with that object in the most nested child

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

0

You could iterate the array and exit early on find, either by finding the node directly or by finding it by iterating the children.

const
    find = (key, array = []) => {
        let node;
        array.some(object => node = object.key === key
            ? object
            : find(key, object.children)
        );
        return node;
    }
    data = [{ title: 'parent 1', key: '0-0', children: [{ title: 'parent 1-0', key: '0-0-0', disabled: true, children: [{ title: 'leafffff', key: '0-0-0-0', disableCheckbox: true }, { title: 'leaf', key: '0-0-0-1' }] }] }];

console.log(find('0-0', data));
console.log(find('0-0-0', data));
console.log(find('0-0-0-0', data));
console.log(find('0-0-0-1', data));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

const treeData = [
  {
    title: 'parent 1',
    key: '0-0',
    children: [
      {
        title: 'parent 1-0',
        key: '0-0-0',
        disabled: true,
        children: [
          { title: 'leafffff', key: '0-0-0-0', disableCheckbox: true },
          { title: 'leaf', key: '0-0' },
        ],
      },
    ],
  },
];
const findNodeInTree = (tree, key) => {
  let result = null;
  tree.forEach(node => {
    if (node.key === key) {
      result = node;
    } else if (node.children) {
      result = findNodeInTree(node.children, key);
    }
  });
  return result;
}
console.log(findNodeInTree(treeData, '0-0-0-0'));

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!