0

Given a data structure like this:

[
  {
    id: 7,
    isOpen: false,
    children: [
      {
        id: 678,
        parentId: 7,
        isOpen: false
      }
    ]
  },
  {
    id: 268,
    isOpen: false
  }
]

How can we implement a function that will return the object that matches a given id?

For example

f(7)

should return

{
  id: 7,
  isOpen: false,
  children: [
    {
      id: 678,
      parentId: 7,
      isOpen: false
    }
  ]
}

and f(678) should return

{
  id: 678,
  parentId: 7,
  isOpen: false
}
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
  • You don't. You write a function that relies on `findIndex` and can recurse without being tacked onto the Array prototype? Read through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex and then write a variation on the example code to effect the necessary recursion – Mike 'Pomax' Kamermans May 06 '19 at 20:05
  • Possible duplicate of [How to find a object in a nested array using recursion in JS](https://stackoverflow.com/questions/53390440) and [JavaScript recursive search in JSON object](https://stackoverflow.com/questions/22222599) and [Find an object in an array of deeply nested objects recursively](https://stackoverflow.com/questions/46062970) and [find object in in array of objects](https://stackoverflow.com/questions/49117845) – adiga May 06 '19 at 20:12

0 Answers0