How to write recursive function javascript push my array property Json object?

Consider this simple function:

function enumerate(x, fn) {
    if (x && typeof x === 'object') {
        for (let val of Object.values(x)) {
            fn(val);
            enumerate(val, fn);
        }
    }
}

It walks through a nested object structure and invokes the given callback for each value it finds (no matter object or primitive). You can apply it to your structure and provide a callback that analyzes the value and, if it has the needed properties, adds it to an array, for example:

enumerate(yourData, function (val) {
    if (val.start_location)
        yourArray.push([val.start_location, val.end_location])
});

Hope this helps you getting started, good luck!

Browse More Popular Posts

Leave a Comment