How to compare values in an array, that is inside an array of objects, with a property of the objects?

Vincent

I've got a graph with nodes that can be connected to multiple other nodes.

Every node is an object in an array. Within every node's object is an array that contains the ids of all the nodes linked to this node and its depth:

const nodes = [
    { "id": 37, "depth": 0, "children": [210, 395, 265], "next": [] },
    { "id": 210, "depth": 1, "children": [37, 260, 259, 391],"next": [] },
    { "id": 256, "depth": 2, "children": [265], "next": [] },
    { "id": 259, "depth": 2, "children": [210, 397, 396], "next": [] },
    { "id": 260, "depth": 2, "children": [210], "next": [] },
    { "id": 265, "depth": 1, "children": [37, 256, 388, 394, 271, 269], "next": [] },
    { "id": 269, "depth": 2, "children": [265], "next": [] },
    { "id": 271, "depth": 2, "children": [265], "next": [] },
    { "id": 388, "depth": 2, "children": [265], "next": [] },
    { "id": 391, "depth": 2, "children": [210], "next": [] },
    { "id": 394, "depth": 2, "children": [265], "next": [] },
    { "id": 395, "depth": 1, "children": [37], "next": [] },
    { "id": 396, "depth": 3, "children": [259, 413], "next": [] },
    { "id": 397, "depth": 3, "children": [259], "next": [] },
    { "id": 413, "depth": 4, "children": [396], "next": [] }
];

I would like to traverse the graph where the node with depth 0 is the root.

The problem is that a node's children array contains all the nodes linked to it. A node with a depth of 2 points back to a node with a depth of 1.

So I would like to create a new array within the nodes' objects, let's say nodes.next and get rid of the children that point back to a node that has a depth lower than itself.

I got it working after a while in two ways. In the first I checked if the length of the children's array is more than 1. Then I relied on the fact that the node in the children's array that should not be pushed to next, happens to be at index 0. That isn't very reliable.

What I found difficult in the second solution is checking if the depth of the nodes in the children's array is higher then the depth of the node in the current iteration. If it is, push it to the node's next array. I hope you can show how to do that in a better way, because this solution isn't pretty by any means:

let currentDepth;
let childDepth;
let currentID;
let childID;

const getChildDepth = (childID) => {
    for (let i = 0; i < nodes.length; i++) {
        if (childID === nodes[i].id) {
            childDepth = nodes[i].depth
        }
    }
};

for (let i = 0; j < nodes.length; j++) {
    currentDepth = nodes[j].depth;
    currentID = nodes[j].id;
    if (nodes[j].children.length > 1) {
        for (let i = 0; i < nodes[j].children.length; i++) {
            childID = nodes[j].children[i];
            getChildDepth(childID);
            if (childDepth > currentDepth) {
                nodes[j].next.push(childID)
            }
        }
    }
}

sample output:

const nodes = [
    { "id": 37, "depth": 0, "children": [210, 395, 265], "next": [210, 395, 265] },
    { "id": 210, "depth": 1, "children": [37, 260, 259, 391],"next": [260, 259, 391] },
    { "id": 256, "depth": 2, "children": [265], "next": [] },
    { "id": 259, "depth": 2, "children": [210, 397, 396], "next": [397, 396] },
    { "id": 260, "depth": 2, "children": [210], "next": [] },
    { "id": 265, "depth": 1, "children": [37, 256, 388, 394, 271, 269], "next": [256, 388, 394, 271, 269] },
    { "id": 269, "depth": 2, "children": [265], "next": [] },
    { "id": 271, "depth": 2, "children": [265], "next": [] },
    { "id": 388, "depth": 2, "children": [265], "next": [] },
    { "id": 391, "depth": 2, "children": [210], "next": [] },
    { "id": 394, "depth": 2, "children": [265], "next": [] },
    { "id": 395, "depth": 1, "children": [37], "next": [] },
    { "id": 396, "depth": 3, "children": [259, 413], "next": [413] },
    { "id": 397, "depth": 3, "children": [259], "next": [] },
    { "id": 413, "depth": 4, "children": [396], "next": [] }
];
Nina Scholz

You could take a Map as reference to the nodes and update next by filtering the children with a depth check.

var nodes = [{ id: 37, depth: 0, children: [210, 395, 265], next: [] }, { id: 210, depth: 1, children: [37, 260, 259, 391], next: [] }, { id: 256, depth: 2, children: [265], next: [] }, { id: 259, depth: 2, children: [210, 397, 396], next: [] }, { id: 260, depth: 2, children: [210], next: [] }, { id: 265, depth: 1, children: [37, 256, 388, 394, 271, 269], next: [] }, { id: 269, depth: 2, children: [265], next: [] }, { id: 271, depth: 2, children: [265], next: [] }, { id: 388, depth: 2, children: [265], next: [] }, { id: 391, depth: 2, children: [210], next: [] }, { id: 394, depth: 2, children: [265], next: [] }, { id: 395, depth: 1, children: [37], next: [] }, { id: 396, depth: 3, children: [259, 413], next: [] }, { id: 397, depth: 3, children: [259], next: [] }, { id: 413, depth: 4, children: [396], next: [] }],
    references = new Map(nodes.map(n => [n.id, n]));

nodes.forEach(node => node.next = node.children.filter(
    id => references.get(id).depth > node.depth
));

console.log(nodes);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript - How to compare property values in objects in an array

compare array values with objects

How to extract values of a property inside an array of objects- JS

How to compare a property value in multiple objects of an array?

How to compare object values in an array of objects?

How to get an array of the values of all properties of objects inside an array of objects?

How to group array of objects by certain property values

How to sort objects in an array inside a json or jsonb value by a property of the objects?

How to delete duplicate values of objects inside array?

How to get all values of objects inside array

how to compare an objects array and a nested objects array?

Sorting an array of objects by property values

Joining property values of objects in an array

compare values of an array, with the key of an array of objects

Compare an array of objects and an array of singular values

How to compare time property in each object group in a array of objects

How to compare and filter objects of array based on property value?

How do i access a property on the objects in an array and compare it?

How to get values from an array, inside another array, with stdClass Objects?

How to compare array of objects to find missing objects

How to compare two array of objects?

How to compare objects in one array

Compare an array with an array of objects

Compare two array lists of objects by property value

Recursive array of property values in array of objects

Compare objects inside an array in Angular 4

How do I compare single array of objects with it key values

How can I compare values between objects in an array?

How to compare a nested array of objects and only return common values