0

An array of nested arrays and objects, each node has a unique value, finding a value on this data, how to get the value on each node?

const opts = [
    {
        value: '01',
        children: [
            { value: '0198' },
            { value: '0195', children: [{ value: '09977' }] }
        ]
    },
    {
        value: '02',
        children: [
            { value: '01986' },
            {
                value: '0195',
                children: [
                    { value: '09978', children: [{ value: '09864' }, { value: '90876' }] }
                ]
            }
        ]
    }
];

const code = '90876';
// expected get an array ['02','0195','09978','90876']
Leroy
  • 56
  • 5
  • should not the values be ```["01","0198","0195","09977","02","01986","0195","09978","09864","90876"]``` ? – grodzi Oct 24 '19 at 10:03
  • I expected the result should be a list of each node value on the only path in the tree structure to the target value – Leroy Oct 24 '19 at 10:09

3 Answers3

2

U could use a function to walk over the object structure recursively like described here:

const opts = [
    {
        value: '01',
        children: [
            { value: '0198' },
            { value: '0195', children: [{ value: '09977' }] }
        ]
    },
    {
        value: '02',
        children: [
            { value: '01986' },
            {
                value: '0195',
                children: [
                    { value: '09978', children: [{ value: '09864' }, { value: '90876' }] }
                ]
            }
        ]
    }
];


function eachRecursive(obj, cb) {
    for (var k in obj) {
        if (typeof obj[k] == "object" && obj[k] !== null)
            eachRecursive(obj[k], cb);
        else
            cb(obj[k]);
    }
}

let results = [];

eachRecursive(opts, val => results.push(val));
console.log(results);

but not sure what you mean with your comment: // expected get an array ['02','0195','0997','90876'] can your explain why you expect that?

Mischa
  • 1,591
  • 9
  • 14
  • sorry, It should be ['02','0195','09978','90876']. That means a list of each node value. – Leroy Oct 24 '19 at 09:59
  • Its still not clear to me why this is the expected answer to ur topic ("How to get the value of each node of the tree structure") ... i think what you actually want is the path to a certain value?! ... if so then have a look at user753642 `s answer. – Mischa Oct 24 '19 at 11:50
1

you can use a dfs algo

function dfs(o, target){
    if(o.value == target) return [target];
    if(!o.children) return false;
    let path;
    o.children.find(x=>path=dfs(x, target));
    if(path){
        return [o.value].concat(path);
    }
};

const opts = [
    {
        value: '01',
        children: [
            { value: '0198' },
            { value: '0195', children: [{ value: '09977' }] }
        ]
    },
    {
        value: '02',
        children: [
            { value: '01986' },
            {
                value: '0195',
                children: [
                    { value: '09978', children: [{ value: '09864' }, { value: '90876' }] }
                ]
            }
        ]
    }
];

let path;
opts.find(x=>path=dfs(x, '90876'))
console.log(path);
grodzi
  • 5,633
  • 1
  • 15
  • 15
0

const opts = [
    {
        value: '01',
        children: [
            { value: '0198' },
            { value: '0195', children: [{ value: '09977' }] }
        ]
    },
    {
        value: '02',
        children: [
            { value: '01986' },
            {
                value: '0195',
                children: [
                    { value: '09978', children: [{ value: '09864' }, { value: '90876' }] }
                ]
            }
        ]
    }
];
console.log(opts[1].value)
console.log(opts[1].children[1].value)
console.log(opts[1].children[1].children[0].value)
console.log(opts[1].children[1].children[0].children[1].value)
AzizStark
  • 1,390
  • 1
  • 17
  • 27