简体   繁体   中英

How to find index of parent or children on nested array?

I am bit stuck in one solution

I have an nested array i would like to find and index of any parent or children using key id, Here is sample json

[
  {
    "id": "1",
    "Name": "John Doe",
    "children": [
      {
        "id": "1.1",
        "name": "John doe 1.1"
      },
      {
        "id:": "1.2",
        "name:": "John doe 1.2"
      },
      {
        "id": "1.3",
        "name": "John doe 1.3",
        "children": [
          {
            "id": "1.3.1",
            "name": "John doe 1.3.1"
          }
        ]
      }
    ]
  },
  {
    "id": "2",
    "Name": "Apple",
    "children": [
      {
        "id": "2.1",
        "name": "Apple 2.1"
      },
      {
        "id:": "1.2",
        "name:": "Apple 1.2"
      }
    ]
  }
]

I want to find an index of parent or child array object to add children of that parent or child

so basically it will be nested array of n levels

Array.find or filter would do for the parent.

let found = data.find(each => each.id == '2');
console.log(found);
let foundChild = found.children.find(child => child.id == '1.2');
console.log(foundChild);

replace the find with filter

Here is a flexible, iterative solution using object-scan

 // const objectScan = require('object-scan'); const myData = [ { id: '1', Name: 'John Doe', children: [ { id: '1.1', name: 'John doe 1.1' }, { 'id:': '1.2', 'name:': 'John doe 1.2' }, { id: '1.3', name: 'John doe 1.3', children: [ { id: '1.3.1', name: 'John doe 1.3.1' } ] } ] }, { id: '2', Name: 'Apple', children: [ { id: '2.1', name: 'Apple 2.1' }, { 'id:': '1.2', 'name:': 'Apple 1.2' } ] } ]; const getNode = (data, id) => objectScan(['**(^children$).id'], { useArraySelector: false, abort: true, rtn: 'parent', filterFn: ({ value }) => value === id })(data); const getNodeWithParents = (data, id) => objectScan(['**(^children$).id'], { useArraySelector: false, abort: true, rtn: 'parents', filterFn: ({ value }) => value === id })(data); console.log(getNode(myData, '2.1')); // => { id: '2.1', name: 'Apple 2.1' } console.log(getNodeWithParents(myData, '2.1')); /* => [ { id: '2.1', name: 'Apple 2.1' }, [ { id: '2.1', name: 'Apple 2.1' }, { 'id:': '1.2', 'name:': 'Apple 1.2' } ], { id: '2', Name: 'Apple', children: [ { id: '2.1', name: 'Apple 2.1' }, { 'id:': '1.2', 'name:': 'Apple 1.2' } ] }, [ { id: '1', Name: 'John Doe', children: [ { id: '1.1', name: 'John doe 1.1' }, { 'id:': '1.2', 'name:': 'John doe 1.2' }, { id: '1.3', name: 'John doe 1.3', children: [ { id: '1.3.1', name: 'John doe 1.3.1' } ] } ] }, { id: '2', Name: 'Apple', children: [ { id: '2.1', name: 'Apple 2.1' }, { 'id:': '1.2', 'name:': 'Apple 1.2' } ] } ] ] */
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="https://bundle.run/object-scan@14.0.0"></script>

Disclaimer : I'm the author of object-scan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM