Multidimensional array inside an object

Go through this javascript object:

var obj = [{
  id: "A",
  children: [{
    id: "B",
    children: [{
      id: "C",
      children: [{
        id: "D",
        children: [{
          id: "E",
          children: [{
            id: "F"
          }]
        }]
      }, {
        id: "G",
        children: {
          id: "H"
        }
      }]
    }, {
      id: "I"
    }]
  }, {
    id: "J",
    children: [{
      id: "K"
    }]
  }]
}, {
  id: "L"
}, {
  id: "M",
  children: {
    id: "N",
    children: [{
      id: "O"
    }]
  }
}, {
  id: "P"
}];

How to write JavaScript code for recursive analysis and print all identifiers on the console so that the result looks like this:

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P

This is how far I could get. After that, I could not think of any logic.

for ( i=0 ; i < obj.length ; i++ ){
         var objId = obj[i];
         for( j=i; j<1 ; j++){
             console.log(obj[j].id);
             console.log(obj[j].children[j].id);
         }
     }

I do not understand what logic should be applied here. Help.

+4
source share
3 answers

You can use an iterative and recursive approach using the depth search algorithm at the beginning .

Edit: advanced for children as an object.

var data = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: { id: "H" } }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: { id: "N", children: [{ id: "O" }] } }, { id: "P" }];

data.forEach(function iter(a) {
    console.log(a.id);
    if (Array.isArray(a.children)) {
        a.children.forEach(iter);
        return;
    }
    if (a.children && typeof a.children === 'object') { // omit this part
        iter(a.children);                               // if children is
    }                                                   // always an array
});
Run code

This version collects all the necessary data and returns them to the array.

var data = [{ id: "A", children: [{ id: "B", children: [{ id: "C", children: [{ id: "D", children: [{ id: "E", children: [{ id: "F" }] }] }, { id: "G", children: { id: "H" } }] }, { id: "I" }] }, { id: "J", children: [{ id: "K" }] }] }, { id: "L" }, { id: "M", children: { id: "N", children: [{ id: "O" }] } }, { id: "P" }],
    result = data.reduce(function iter(r, o) {
        r.push(o.id);
        if (Array.isArray(o.children)) {
            return o.children.reduce(iter, r);
        }
        if (o.children && typeof o.children === 'object') { // omit this part
            return iter(r, o.children);                     // if children is
        }                                                   // always an array
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run code
+5
source

ES6. , children , , , . , .

function getIds(data) {
    return data.reduce((acc, el) => acc.concat(el.id, getIds(el.children || [])), [])
}

var obj = [{
  id: "A",
  children: [{
    id: "B",
    children: [{
      id: "C",
      children: [{
        id: "D",
        children: [{
          id: "E",
          children: [{
            id: "F"
          }]
        }]
      }, {
        id: "G",
        children: [{
          id: "H"
        }]
      }]
    }, {
      id: "I"
    }]
  }, {
    id: "J",
    children: [{
      id: "K"
    }]
  }]
}, {
  id: "L"
}, {
  id: "M",
  children: [{
    id: "N",
    children: [{
      id: "O"
    }]
  }]
}, {
  id: "P"
}];

console.log(getIds(obj).join('\n'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
+1

What you are showing seems to be asking for the first depth solution, since the order of the identifiers is clearly alphabetical, and they are ordered by first meeting in depth.

As a result, each identifier found will be collected, and then deeper identifiers will be examined. This can and probably should be done with recursion.

Here is an example.

  
var obj=[{id:"A",children:[{id:"B",children:[{id:"C",children:[{id:"D",children:[{id:"E",children:[{id:"F"}]}]},{id:"G",children:{id:"H"}}]},{id:"I"}]},{id:"J",children:[{id:"K"}]}]},{id:"L"},{id:"M",children:{id:"N",children:[{id:"O"}]}},{id:"P"}];

var ids = [];//output holder
(function tree(cur){ //recursive function
 for(var i = 0; i < cur.length; i++){ //iterate current set
     ids.push(cur[i].id); //always store id
     if(cur[i].children) tree(cur[i].children); //recurse if children exist
 }
})(obj) //start from the top
    
console.log(ids);
Run code
0
source

Source: https://habr.com/ru/post/1663782/


All Articles