Search nested object and return whole path - javascript

I have below JavaScript with n level children and want to search for id and if any of item from has matching id than need to return object from root to matching item.
I want to return entire hierarchy of found item from root till object with it's children.
I tried with lodash and underscore and could not find easy solution.
input: {
"children": [{
"name": "Home",
"title": "Home",
"id": "home1",
"children": []
},
{
"name": "BUSINESS AND ROLE SPECIFIC",
"title": "BUSINESS AND ROLE SPECIFIC",
"id": "BAR1",
"children": [{
"name": "Global Businesses",
"title": "Global Businesses",
"id": "GB1",
"children": [{
"name": "Commercial Banking",
"title": "Commercial Banking",
"id": "CB1",
"children": [{
"name": "FLAGSHIP PROGRAMMES",
"title": "FLAGSHIP PROGRAMMES",
"id": "FG1",
"children": []
}]
}]
}]
},
{
"name": "RISK MANAGEMENT",
"title": "RISK MANAGEMENT",
"id": "RM1",
"children": []
}
]
}
Search: {
id: 'FG1'
}
return :{
"name": "BUSINESS AND ROLE SPECIFIC",
"title": "BUSINESS AND ROLE SPECIFIC",
"id": "BAR1",
"children": [{
"name": "Global Businesses",
"title": "Global Businesses",
"id": "GB1",
"children": [{
"name": "Commercial Banking",
"title": "Commercial Banking",
"id": "CB1",
"children": [{
"name": "FLAGSHIP PROGRAMMES",
"title": "FLAGSHIP PROGRAMMES",
"id": "FG1",
"children": [{}]
}]
}]
}]
}

You could use this function:
function findChild(obj, condition) {
if (Object.entries(condition).every( ([k,v]) => obj[k] === v )) {
return obj;
}
for (const child of obj.children || []) {
const found = findChild(child, condition);
// If found, then add this node to the ancestors of the result
if (found) return Object.assign({}, obj, { children: [found] });
}
}
// Sample data
var input = { "children": [{ "name": "Home", "title": "Home", "id": "home1", "children": [] }, { "name": "BUSINESS AND ROLE SPECIFIC", "title": "BUSINESS AND ROLE SPECIFIC", "id": "BAR1", "children": [{ "name": "Global Businesses", "title": "Global Businesses", "id": "GB1", "children": [{ "name": "Commercial Banking", "title": "Commercial Banking", "id": "CB1", "children": [{ "name": "FLAGSHIP PROGRAMMES", "title": "FLAGSHIP PROGRAMMES", "id": "FG1", "children": [] }] }] }] }, { "name": "RISK MANAGEMENT", "title": "RISK MANAGEMENT", "id": "RM1", "children": [] } ]},
search = { id: 'FG1' };
console.log(findChild(input, search));
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use this also for searching with multiple conditions, which must be true at the same time:
search = { "name": "Global Businesses", "title": "Global Businesses" };
... would give you the object that has the specified name and title.
Follow-up question
You asked in comments:
Is there way to supply number to not remove children for given node in input. like,
const donotRemoveChildNode = 2;
console.log(findChild(input, search, donotRemoveChildNode ));
...so it will not remove that specific node's children if it matches condition?
Here, if we search for { id: 'FG1'} and supply donotRemoveChildNode = 2, it would not remove the first level children for "Commercial banking".
I would say the donotRemoveChildNode would have to be 3, as there are three levels of children arrays in the ancestor-hierarchy of the "Commercial banking" node. A value of 0 would show the first level children of the top-most children property.
Here is how that extra argument would work -- I added some records to the data to illustrate the difference in the output:
function findChild(obj, condition, removeChildNodesBefore = Infinity) {
if (Object.entries(condition).every( ([k,v]) => obj[k] === v )) {
return obj;
}
for (const child of obj.children || []) {
let found = findChild(child, condition, removeChildNodesBefore - 1);
if (found) {
return Object.assign({}, obj, {
children: removeChildNodesBefore <= 0
? obj.children.map( sibling =>
sibling == child ? found
: Object.assign({}, sibling, {children: []})
)
: [found]
});
}
}
}
var input = { "children": [{ "name": "Home", "title": "Home", "id": "home1", "children": [] }, { "name": "BUSINESS AND ROLE SPECIFIC", "title": "BUSINESS AND ROLE SPECIFIC", "id": "BAR1", "children": [{ "name": "Global Businesses", "title": "Global Businesses", "id": "GB1", "children": [{ "name": "test", "title": "test", "id": "xxx", "children": [{ "name": "testDeep", "title": "test", "id": "deep", "children": []}]}, { "name": "Commercial Banking", "title": "Commercial Banking", "id": "CB1", "children": [{ "name": "test", "title": "test", "id": "yyy", "children": []}, { "name": "FLAGSHIP PROGRAMMES", "title": "FLAGSHIP PROGRAMMES", "id": "FG1", "children": [] }] }] }] }, { "name": "RISK MANAGEMENT", "title": "RISK MANAGEMENT", "id": "RM1", "children": [] } ]},
search = { id: 'FG1' }
console.log(findChild(input, search, 3));
.as-console-wrapper { max-height: 100% !important; top: 0; }

function getBranch(branches, leaf_id)
{
var result_branch = null;
branches.some(function(branch, idx) {
if (branch.id == leaf_id) {
result_branch = Object.assign({}, branch);
result_branch.children.forEach(function(child, idx) {
delete result_branch.children[idx].children;
});
return true;
} else {
let target_branch = getBranch(branch.children, leaf_id);
if (target_branch) {
result_branch = Object.assign({}, branch);
delete result_branch.children
result_branch.children = [target_branch];
return true;
}
}
return false;
});
return result_branch;
}
console.log(getBranch(input.children, 'GB1'));

One way is to first loop the root children, and then create another function to see if the Id exists in any of it's children.
var data = {
"children": [{
"name": "Home",
"title": "Home",
"id": "home1",
"children": []
},
{
"name": "BUSINESS AND ROLE SPECIFIC",
"title": "BUSINESS AND ROLE SPECIFIC",
"id": "BAR1",
"children": [{
"name": "Global Businesses",
"title": "Global Businesses",
"id": "GB1",
"children": [{
"name": "Commercial Banking",
"title": "Commercial Banking",
"id": "CB1",
"children": [{
"name": "FLAGSHIP PROGRAMMES",
"title": "FLAGSHIP PROGRAMMES",
"id": "FG1",
"children": []
}]
}]
}]
},
{
"name": "RISK MANAGEMENT",
"title": "RISK MANAGEMENT",
"id": "RM1",
"children": []
}
]
};
function hasId( id, data ) {
if (data.id === id) return true;
if (data.children) {
for (const child of data.children) {
if (hasId( id, child)) return true;
}
}
return false;
}
function search( id, data ) {
for (const child of data.children) {
if (hasId(id, child)) return child;
}
return null;
}
console.log(search( "FG1", data ));

Related

Extract the parent node name from Tree who has childrens

I want to iterate the tree and need to get the id of all the nodes which has the children in string array. while looping it is just returning me the record but doesn't extract the name of the node.
e.g const result = ['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY'];
{
"id": "root",
"name": "Roles and Permissions",
"children": [
{
"id": "USER",
"name": "USER",
"children": [
{
"id": "1",
"name": "VIEW"
},
{
"id": "2",
"name": "CREATE"
},
{
"id": "3",
"name": "EDIT"
}
]
},
{
"id": "ROLE",
"name": "ROLE",
"children": [
{
"id": "8",
"name": "VIEW"
},
{
"id": "9",
"name": "CREATE"
},
{
"id": "10",
"name": "EDIT"
},
{
"id": "11",
"name": "DELETE"
}
]
},
{
"id": "DASHBOARD",
"name": "DASHBOARD",
"children": [
{
"id": "BRAND",
"name": "BRAND",
"children": [
{
"id": "52",
"name": "VIEW"
},
{
"id": "53",
"name": "CREATE"
},
{
"id": "54",
"name": "EDIT"
},
{
"id": "55",
"name": "DELETE"
}
]
},
{
"id": "COMPANY",
"name": "COMPANY",
"children": [
{
"id": "56",
"name": "VIEW"
},
{
"id": "57",
"name": "CREATE"
},
{
"id": "58",
"name": "EDIT"
},
{
"id": "59",
"name": "DELETE"
}
]
}
]
}
]
}
I tried various looping method to get the list, e.g. but not returning the exact name of the node.
function getParent(nodes) {
if(Array.isArray(nodes.children)) {
return nodes.children.map((node) => getParent(node));
}
return nodes.name;
}
You can store the resp in an array and return that array.
const q = {
"id": "root",
"name": "Roles and Permissions",
"children": [
{
"id": "USER",
"name": "USER",
"children": [
{
"id": "1",
"name": "VIEW"
},
{
"id": "2",
"name": "CREATE"
},
{
"id": "3",
"name": "EDIT"
}
]
},
{
"id": "ROLE",
"name": "ROLE",
"children": [
{
"id": "8",
"name": "VIEW"
},
{
"id": "9",
"name": "CREATE"
},
{
"id": "10",
"name": "EDIT"
},
{
"id": "11",
"name": "DELETE"
}
]
},
{
"id": "DASHBOARD",
"name": "DASHBOARD",
"children": [
{
"id": "BRAND",
"name": "BRAND",
"children": [
{
"id": "52",
"name": "VIEW"
},
{
"id": "53",
"name": "CREATE"
},
{
"id": "54",
"name": "EDIT"
},
{
"id": "55",
"name": "DELETE"
}
]
},
{
"id": "COMPANY",
"name": "COMPANY",
"children": [
{
"id": "56",
"name": "VIEW"
},
{
"id": "57",
"name": "CREATE"
},
{
"id": "58",
"name": "EDIT"
},
{
"id": "59",
"name": "DELETE"
}
]
}
]
}
]
}
let result = []
function r(nodes){
if(Array.isArray(nodes.children)){
result.push(nodes.name);
nodes.children.map((c) => r(c))
return result;
}
return result;
}
console.log(r(q))
You can simply use a recursive function. Here ids is an array. You can initialize it before calling the function. Call this function in your getting IDs method.
const getIdFromNodesWithChild = (node) => {
if (node.children != undefined){
ids.push(node.id)
const children_list = node.children
children_list.forEach( new_child => getIdFromNodesWithChild(new_child))
}}
caller function
const returnIds = (tree) => {
ids = []
getIdFromNodesWithChild(tree)
return (ids)
}
result : ['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY']

angular 3 nested object

Can someone help me regarding my code I already search but had no luck on logic.
i am trying to get a nested drop but i get the same result on 3-child hierarchy.
this is the data from my API.
{
"data": [
{
"id": "1",
"name": "Metro Manila",
"parent": null
},
{
"id": "101",
"name": "Manila",
"parent": "1"
},
{
"id": "10101",
"name": "Malate",
"parent": "101"
},
{
"id": "10102",
"name": "Ermita",
"parent": "101"
},
{
"id": "10103",
"name": "Binondo",
"parent": "101"
},
{
"id": "102",
"name": "Makati",
"parent": "1"
},
{
"id": "10201",
"name": "Poblacion",
"parent": "102"
},
{
"id": "10202",
"name": "Bel-Air",
"parent": "102"
},
{
"id": "10203",
"name": "San Lorenzo",
"parent": "102"
},
{
"id": "10204",
"name": "Urdaneta",
"parent": "102"
},
{
"id": "103",
"name": "Marikina",
"parent": "1"
},
{
"id": "10301",
"name": "Sto Nino",
"parent": "103"
},
{
"id": "10302",
"name": "Malanday",
"parent": "103"
},
{
"id": "10303",
"name": "Concepcion I",
"parent": "103"
},
{
"id": "2",
"name": "CALABARZON",
"parent": null
},
{
"id": "201",
"name": "Laguna",
"parent": "2"
},
{
"id": "20101",
"name": "Calamba",
"parent": "201"
},
{
"id": "20102",
"name": "Sta. Rosa",
"parent": "201"
},
{
"id": "202",
"name": "Cavite",
"parent": "2"
},
{
"id": "20201",
"name": "Kawit",
"parent": "202"
},
{
"id": "203",
"name": "Batangas",
"parent": "2"
},
{
"id": "20301",
"name": "Lipa",
"parent": "203"
},
{
"id": "20302",
"name": "Tanauan",
"parent": "203"
},
{
"id": "3",
"name": "Central Luzon",
"parent": null
},
{
"id": "301",
"name": "Bulacan",
"parent": "3"
},
{
"id": "302",
"name": "Nueva Ecija",
"parent": "3"
},
{
"id": "303",
"name": "Tarlac",
"parent": "3"
},
{
"id": "304",
"name": "Pampanga",
"parent": "3"
}
]
}
this.data = result.body.data;
let parents = this.data.filter(x => x.parent == null);
let child_id = [];
let child_id2 = [];
for (let i = 0; i < parents.length; i++) {
let _myTreelist = new ParentData();
_myTreelist.data.parent = parents[i].name;
child_id = this.data.filter(x => x.parent == parents[i].id); //get child-1 with id
_myTreelist.data.child.child1 = child_id.map((item) => {
return item.name
})
for (let e = 0; e < child_id.length; e++) { //10 ids
child_id2 = this.data.filter(a => a.parent === child_id[e].id); //get child-2 with id
_myTreelist.data.child.child.child2 = child_id2.map((item) => {
return item.name
})
}
this.parentList.push(_myTreelist);
}
this is the image output I get.
it works the first and 2nd nested but in the 3rd it display same
make a recursive function
getChild(element:any,data:any[])
{
element.children=data.filter((x:any)=>x.parent==element.id)
if (element.children.length)
element.children.forEach((x:any)=>this.getChild(x,data))
else
element.children=null;
return element
}
then
treeData=this.data.filter(x=>!x.parent)
.map(x=>this.getChild(x,this.data))
stackblitz
If you use an API and an observable use pipe map
treeData$=this.service.getData().pipe(
map((data:any[])=>{
return data.filter(x=>!x.parent).map(x=>this.getChild(x,data))
})
)

Need to add parentId to child in multi-dimensional Array of JSON data

I have a multidimensional array of JSON data with 'n' number of nested children. My task is to add UniqueId to parent JSON and that uniqueId should be added as parentId to the child. Can you please help in javascript. Thanks
Note:
The number of child its recursive, and there can be any number of children. For this purpose, we can have a deep level of three
Input :
[{
"text": 1527978678434,
"value": 1527978678434,
"children": [{
"text": 1292232152442,
"value": 1292232152442,
"children": [{
"text": 474194771845,
"value": 474194771845,
"children": []
},
{
"text": 468086178830,
"value": 468086178830,
"children": []
}
]
},
{
"text": 1067869237589,
"value": 1067869237589,
"children": []
},
{
"text": 1166591731429,
"value": 1166591731429,
"children": []
},
]
}]
The Required Output:
[{
"text": 1527978678434,
"value": 1527978678434,
"parentId": 0,
"uniqueId": 1,
"children": [{
"text": 1292232152442,
"value": 1292232152442,
"parentId": 1,
"uniqueId": 2,
"children": [{
"text": 474194771845,
"value": 474194771845,
"parentId": 2,
"uniqueId": 3,
"children": []
},
{
"text": 468086178830,
"value": 468086178830,
"parentId": 2,
"uniqueId": 4,
"children": []
}
]
},
{
"text": 1067869237589,
"value": 1067869237589,
"parentId": 1,
"uniqueId": 5,
"children": []
},
{
"text": 1166591731429,
"value": 1166591731429,
"parentId": 1,
"uniqueId": 6,
"children": []
},
{
"text": 111221786011,
"value": 111221786011,
"parentId": 1,
"uniqueId": 7,
"children": []
},
{
"text": 641372005975,
"value": 641372005975,
"parentId": 1,
"uniqueId": 8,
"children": [{
"text": 23082640100,
"value": 23082640100,
"parentId": 8,
"uniqueId": 9,
"children": []
}]
}
]
}]
Consider data is your array.
Just do the DFS and alter the data on the fly. + Having a global to hold the increased ID.
let lastUniqueId = 0;
function addIds(children, parentId) {
(children || []).forEach(r => {
r.parentId = parentId;
r.uniqueId = ++lastUniqueId;
addIds(r.children, r.uniqueId);
});
}
addIds(data, lastUniqueId);
This seems something you can do using a recursive function while keeping track of a shared uniqueId counter, assuming the nesting doesn't go incredibly deep:
const data = [{
"text": 1527978678434,
"value": 1527978678434,
"children": [{
"text": 1292232152442,
"value": 1292232152442,
"children": [{
"text": 474194771845,
"value": 474194771845,
"children": []
},
{
"text": 468086178830,
"value": 468086178830,
"children": []
}
]
},
{
"text": 1067869237589,
"value": 1067869237589,
"children": []
},
{
"text": 1166591731429,
"value": 1166591731429,
"children": []
},
]
}];
function convert(array) {
let uniqueId = 0;
function mapData(data, parentId) {
const myId = ++uniqueId;
const result = {
...data, // copy all data
uniqueId, parentId, // add our new fields
// and handle the children recursively
children: data.children.map(c => mapData(c, myId)),
};
// Don't mind this, just tricking JS in displaying the children array at the bottom during console.log
const children = result.children; delete result.children; result.children = children;
return result;
}
return array.map(d => mapData(d, 0));
}
const result = convert(data);
console.log(result);

Add new property in object recursively

I have a recursive array with same structure of objects and it contains name property. My requirement is to add new property id along with name in recursive array of objects
below is my sample array
[
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "ID01",
"type": "Under"
},
{
"children": [],
"name": "ID02",
"type": "Under"
}
],
"name": "httpgateway",
"type": "Gut"
},
{
"children": [
{
"children": [],
"name": "mock1",
"type": "Under"
},
{
"children": [],
"name": "mock2",
"type": "Under"
}
],
"name": "mock",
"type": "Gut"
}
],
"name": "23131",
"type": "SEV"
}
],
"name": "integration",
"type": "DataCenter"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "data1",
"type": "Under"
},
{
"children": [],
"name": "data12",
"type": "Under"
},
{
"children": [],
"name": "data13",
"type": "Under"
},
{
"children": [],
"name": "data14",
"type": "Under"
}
],
"name": "Gut1",
"type": "Gut"
}
],
"name": "213213",
"type": "SEV"
}
],
"name": "dev",
"type": "dt"
}
]
I need Id property along with name as belo
[
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "ID01",
"id": "ID01",
"type": "Under"
},
{
"children": [],
"name": "ID02",
"id": "ID02",
"type": "Under"
}
],
"name": "gate",
"id": "gate",
"type": "Gut"
},
{
"children": [
{
"children": [],
"name": "mock1",
"id": "mock1",
"type": "Under"
},
{
"children": [],
"name": "mock2",
"id": "mock2",
"type": "Under"
}
],
"name": "mock",
"name": "id",
"type": "Gut"
}
],
"name": "23131",
"id": "23131",
"type": "SEV"
}
],
"name": "int",
"id": "int",
"type": "dt"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"name": "data1",
"id": "data1",
"type": "Under"
},
{
"children": [],
"name": "data12",
"id": "data12",
"type": "Under"
}
],
"name": "Gut1",
"id": "Gut1",
"type": "Gut"
}
],
"name": "213213",
"id": "213213",
"type": "SEV"
}
],
"name": "dev",
"id": "dev",
"type": "dt"
}
]
I have written method to update this but its not working as expected
const getTreeItemsFromData = (treeItems) => {
console.log('---------------------------', treeItems)
let finalData = []
return treeItems.map((treeItemData) => {
let children = undefined;
if (treeItemData.children && treeItemData.children.length > 0) {
children = this.getTreeItemsFromData(treeItemData.children);
}
let uniqueId = `${treeItemData.name}${Math.floor(Math.random()*(999-100+1)+100)}`;
finalData.push(treeItemData)
console.log("-- ------------------", treeItemData)
});
};
You jus need a function to accept the array and check if the key children exists and is array, make changes to it and then recursively call if it has children.
const t = [
{
"children": [
{
"children": [
{
"children": [
{
"children": [
],
"name": "data1",
"type": "Under"
},
{
"children": [
],
"name": "data12",
"type": "Under"
},
{
"children": [
],
"name": "data13",
"type": "Under"
},
{
"children": [
],
"name": "data14",
"type": "Under"
}
],
"name": "Gut1",
"type": "Gut"
}
],
"name": "213213",
"type": "SEV"
}
],
"name": "dev",
"type": "dt"
}
];
function addIdRec(arr){
arr.forEach(a => {
if(a.children instanceof Array){
a.id = a.name;
if(a.children.length > 0){
addIdRec(a.children);
}
}
})
}
addIdRec(t)
We can do this with a pretty simple recursion:
const addId = (data) =>
data .map (({name, children, ...rest}) =>
({children: addId(children), name, id: name, ...rest})
)
const data = [{children: [{children: [{children: [{children: [], name: "ID01", type: "Under"}, {children: [], name: "ID02", type: "Under"}], name: "httpgateway", type: "Gut"}, {children: [{children: [], name: "mock1", type: "Under"}, {children: [], name: "mock2", type: "Under"}], name: "mock", type: "Gut"}], name: "23131", type: "SEV"}], name: "integration", type: "DataCenter"}, {children: [{children: [{children: [{children: [], name: "data1", type: "Under"}, {children: [], name: "data12", type: "Under"}, {children: [], name: "data13", type: "Under"}, {children: [], name: "data14", type: "Under"}], name: "Gut1", type: "Gut"}], name: "213213", type: "SEV"}], name: "dev", type: "dt"}]
console .log (addId (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
We simply clone the node, adding an id property to match the name one and recur on the children property.

remove nested null values from javascript array

My javascript array is as below. I want to remove all the null values inside all the children arrays. I managed to remove as below. but i'm looking for more elegant solution other than this
let data = [
{
"id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
"name": "organization 1",
"type": "org",
"title": "organization 1",
"children": [
null,
{
"id": "6571cada-490c-41db-97e8-197a9c0faabb",
"name": "location 3",
"org_id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
"type": "location",
"title": "location 3",
"children": [
null,
{
"id": "8620fce9-f7d0-442a-86e8-f58e9029a164",
"name": "zone 3",
"zone_settings_id": null,
"location_id": "6571cada-490c-41db-97e8-197a9c0faabb",
"type": "zone",
"title": "zone 3",
"children": [
null,
null,
null
]
},
null,
null
]
},
{
"id": "93b8ad9e-59ee-4de5-ac32-d3d5d19b083c",
"name": "location 4",
"org_id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
"type": "location",
"title": "location 4",
"children": [
null,
null,
{
"id": "db14daf4-4488-47fa-8d18-2d213b3a54a5",
"name": "zone 4",
"zone_settings_id": null,
"location_id": "93b8ad9e-59ee-4de5-ac32-d3d5d19b083c",
"type": "zone",
"title": "zone 4",
"children": [
null,
null,
{
"id": "6ae5b04a-1101-4d73-80e4-05d4db454406",
"gwId": "E4956E45107R",
"zone_id": "db14daf4-4488-47fa-8d18-2d213b3a54a5",
"org_id": "359816ba-4bc6-4b7f-b57c-d80331eee0a6",
"title": "E4:95:6E:45:10:7R"
}
]
},
{
"id": "c01398c6-7650-426b-936d-6b88b1b507f2",
"name": "zone 5",
"zone_settings_id": null,
"location_id": "93b8ad9e-59ee-4de5-ac32-d3d5d19b083c",
"type": "zone",
"title": "zone 5",
"children": [
null,
null,
null
]
}
]
},
null
]
},
{
"id": "46665d49-020d-411f-9f11-c9ddad9a741c",
"name": "organization 2",
"type": "org",
"title": "organization 2",
"children": [
null,
null,
null,
null
]
},
{
"id": "95e7d05b-fe67-422d-8617-9f10633ea6f6",
"name": "org 3",
"type": "org",
"title": "org 3",
"children": [
null,
null,
null,
{
"id": "0a8509d8-1fd8-486c-8457-3ff393c09abc",
"name": "location 1 of org 3",
"org_id": "95e7d05b-fe67-422d-8617-9f10633ea6f6",
"type": "location",
"title": "location 1 of org 3",
"children": [
null,
null,
null,
null
]
}
]
}
]
let orgs = data.filter(org => org != null);
orgs.forEach(org => {
org.children = org.children.filter(location => location != null);
})
orgs.forEach(org => {
org.children.forEach(loc => {
loc.children = loc.children.filter(zone => zone != null);
})
})
orgs.forEach(org => {
org.children.forEach(loc => {
loc.children.forEach(zone => {
zone.children = zone.children.filter(router => router != null);
})
})
})
console.log(orgs);
const noNull = array => array
.filter(it => it !== null)
.map(it => it.children ? { ...it, children: noNull(it.children) } : it);
const result = noNull(data);
You could recursively filter the arrays in an immutable way.
Or the mutating version would be:
const noNull = array => {
const result = array.filter(it => it !== null);
for(const value of result)
if(value.children) value.children = noNull(value.children);
return result;
};

Categories

Resources