So I have an array of objects which have all nested children property. It is in front-end a treeview, which should expand the nodes until selected ones, for each id in a list. To be able to do this, I have to get all the parents ids for each selected id from the list.
For example, my list of checkedKeys ids:
[16787217, 16787245, 16787266, 16787270, 16787272, 16787265, 16787264]
All the checked items represents the ids from the list object:
My object list looks like this:
[
{
"id": 11,
"name": "Forecast source",
"value": null,
"children": []
},
{
"id": 2,
"name": "Item Type",
"value": null,
"children": []
},
{
"id": 16787217,
"name": "Item#Cust",
"value": null,
"children": [
{
"id": 16787230,
"name": "Customer",
"value": null,
"children": [
{
"id": 16787291,
"name": "Commercial Network",
"value": null,
"children": []
},
{
"id": 16787296,
"name": "Distribution Site",
"value": null,
"children": []
},
{
"id": 16787265,
"name": "Site",
"value": null,
"children": []
}
]
},
{
"id": 16787254,
"name": "Item",
"value": null,
"children": [
{
"id": 16787294,
"name": "ABC (Regular)",
"value": null,
"children": []
},
{
"id": 16787273,
"name": "ABC (U)",
"value": null,
"children": []
},
{
"id": 16787278,
"name": "ABC (€)",
"value": null,
"children": []
},
{
"id": 16787290,
"name": "Class",
"value": null,
"children": []
},
{
"id": 16787260,
"name": "Family",
"value": null,
"children": [
{
"id": 16787264,
"name": "Product line",
"value": null,
"children": []
}
]
},
{
"id": 16787263,
"name": "Flavour",
"value": null,
"children": []
},
{
"id": 16787262,
"name": "Format",
"value": null,
"children": []
},
{
"id": 16787261,
"name": "Group 1",
"value": null,
"children": []
},
{
"id": 16787292,
"name": "ProdGroup",
"value": null,
"children": []
},
{
"id": 16787293,
"name": "Recipe",
"value": null,
"children": []
},
{
"id": 16787288,
"name": "Sale status",
"value": null,
"children": []
}
]
},
{
"id": 16787245,
"name": "Item#Site",
"value": null,
"children": [
{
"id": 16787266,
"name": "Family#Warehouse",
"value": null,
"children": []
}
]
}
]
},
{
"id": 3,
"name": "Lead Time",
"value": null,
"children": []
},
{
"id": 5,
"name": "Levels",
"value": null,
"children": []
},
{
"id": 16787268,
"name": "N1",
"value": null,
"children": [
{
"id": 16787269,
"name": "N2",
"value": null,
"children": [
{
"id": 16787270,
"name": "N3",
"value": null,
"children": [
{
"id": 16787271,
"name": "N4",
"value": null,
"children": [
{
"id": 16787272,
"name": "N5",
"value": null,
"children": [
{
"id": 33564497,
"name": "N6",
"value": null,
"children": [
{
"id": 33564498,
"name": "N7",
"value": null,
"children": [
{
"id": 33564499,
"name": "N8",
"value": null,
"children": [
{
"id": 33564500,
"name": "N9",
"value": null,
"children": []
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"id": 16787286,
"name": "Op set",
"value": null,
"children": []
}
]
So my problem is that I can't figure out how to get all parents nodes ids recursively until the root level and push them into the expandedKeys array.
I tried to implement a function like this:
this.list.forEach(el => {
this.setExpandedNodes(el);
});
setExpandedNodes(node: PropertyNode) {
if (node.children) {
node.children.forEach(chld => {
this.checkedKeys.forEach(key => {
if (chld.id === key) {
this.expandedKeys.push(node.id);
} else if (chld.children) {
chld.children.forEach(grChld => {
this.setExpandedNodes(grChld);
});
}
});
});
}
}
But I can't figure out how to get all parent ids starting from each selected id until the root level. Anyone have an idea?
Thanks to this answer I managed to do it.
getPath(model, id) {
let path,
item = model.id ;
if (!model || typeof model !== 'object') {
return;
}
if (model.id === id) {
return [item];
}
(model.children || []).some(child => (path = this.getPath(child, id)));
return path && [item, ...path];
}
setExpandedKeys() {
if (this.checkedKeys && this.checkedKeys.length > 0) {
this.checkedKeys.forEach(k => {
this.list.forEach(
mt => {
const result = this.getPath(mt, k);
if (result) {
this.expandedKeys.push(...result);
}
}
);
});
}
}
this.setExpandedKeys();
I have now all the parents ids until the root.
A function that returns an array of parent objects may look like this:
Using tree-model-js:
const getParentsById = (id, data) => {
var TreeModel = require('tree-model')
const tree = new TreeModel()
let path
for (let item of data) {
const root = tree.parse(item)
root.walk(function (node) {
// Halt the traversal by returning false
if (node.model.id === id) {
path = node.getPath()
return false;
}
});
}
path.pop()
return path.map(item => item.model)
}
Without using tree-model-js:
const getParentsById = (id, data) => {
const isFoundChild = (id, data, parents) => {
if (data.find(item => item.id == id)) {
return true;
}
else {
for (let item of data) {
if (item.children.length)
if (isFoundChild(id, item.children)) {
parents.push(item);
return true;
}
}
return false;
}
}
const parents = [];
if (data.find(item => item.id == id))
return [];
else {
for (let item of data) {
if (item.children.length)
if (isFoundChild(id, item.children, parents)) {
parents.push(item);
return parents;
}
}
}
}
Next, it is enough to apply map() to the result:
let parentsId = getParentsById(16787296, data).map(item => item.id)
I am using the pokemon API to build a fun little informational app. I specifically want to have a section detailing the damage relationships of a pokemon. I currently retrieve and format the data as such:
Array [
Object {
"double_damage_from": Array [
Object {
"name": "flying",
"url": "https://pokeapi.co/api/v2/type/3/",
},
Object {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
Object {
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/",
},
Object {
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/",
},
Object {
"name": "ice",
"url": "https://pokeapi.co/api/v2/type/15/",
},
],
"double_damage_to": Array [
Object {
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
Object {
"name": "rock",
"url": "https://pokeapi.co/api/v2/type/6/",
},
Object {
"name": "water",
"url": "https://pokeapi.co/api/v2/type/11/",
},
],
"half_damage_from": Array [
Object {
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
Object {
"name": "water",
"url": "https://pokeapi.co/api/v2/type/11/",
},
Object {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
Object {
"name": "electric",
"url": "https://pokeapi.co/api/v2/type/13/",
},
],
"half_damage_to": Array [
Object {
"name": "flying",
"url": "https://pokeapi.co/api/v2/type/3/",
},
Object {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
Object {
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/",
},
Object {
"name": "steel",
"url": "https://pokeapi.co/api/v2/type/9/",
},
Object {
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/",
},
Object {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
Object {
"name": "dragon",
"url": "https://pokeapi.co/api/v2/type/16/",
},
],
"name": "grass",
"no_damage_from": Array [],
"no_damage_to": Array [],
},
Object {
"double_damage_from": Array [
Object {
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
Object {
"name": "psychic",
"url": "https://pokeapi.co/api/v2/type/14/",
},
],
"double_damage_to": Array [
Object {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
Object {
"name": "fairy",
"url": "https://pokeapi.co/api/v2/type/18/",
},
],
"half_damage_from": Array [
Object {
"name": "fighting",
"url": "https://pokeapi.co/api/v2/type/2/",
},
Object {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
Object {
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/",
},
Object {
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
Object {
"name": "fairy",
"url": "https://pokeapi.co/api/v2/type/18/",
},
],
"half_damage_to": Array [
Object {
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
Object {
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
Object {
"name": "rock",
"url": "https://pokeapi.co/api/v2/type/6/",
},
Object {
"name": "ghost",
"url": "https://pokeapi.co/api/v2/type/8/",
},
],
"name": "poison",
"no_damage_from": Array [],
"no_damage_to": Array [
Object {
"name": "steel",
"url": "https://pokeapi.co/api/v2/type/9/",
},
],
},
]
What I want to do is format it like this.
DamageMap: Map {
"double_damage_from" => Array [
"flying",
"poison",
"bug",
"fire",
"ice",
"ground",
"psychic",
],
"double_damage_to" => Array [
"ground",
"rock",
"water",
"grass",
"fairy",
],
"half_damage_from" => Array [
"ground",
"water",
"grass",
"electric",
"fighting",
"poison",
"bug",
"fairy",
],
"half_damage_to" => Array [
"flying",
"poison",
"bug",
"steel",
"fire",
"grass",
"dragon",
"ground",
"rock",
"ghost",
],
"no_damage_from" => Array [],
"no_damage_to" => Array [
"steel",
],
}
The data is formatted into this map with no duplicate types. This is my current solution.
const keys = [
"double_damage_from",
"double_damage_to",
"half_damage_from",
"half_damage_to",
"no_damage_from",
"no_damage_to",
];
const damageMap = new Map();
for (let i = 0; i < results.length; ++i) {
for (let j = 0; j < keys.length; ++j) {
if (!damageMap.has(keys[j])) {
damageMap.set(keys[j], []);
}
const val = damageMap.get(keys[j]);
const curr = results[i][keys[j]];
for (let k = 0; k < curr.length; ++k) {
if (val.indexOf(curr[k].name) === -1) {
val.push(curr[k].name);
}
}
damageMap.set(keys[j], val);
}
}
return damageMap;
};
It is utterly atrocious... I know this. My problem is that any attempt at optimizing it has so far failed. I have tried using combinations of map and reduce functions to no avail. If anybody can take a look at this and optimize it, it would be greatly appreciated!
You may use Array.prototype.reduce() together with Array.prototype.forEach() instead:
const src = [{"double_damage_from":[{"name":"flying","url":"https://pokeapi.co/api/v2/type/3/",},{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"bug","url":"https://pokeapi.co/api/v2/type/7/",},{"name":"fire","url":"https://pokeapi.co/api/v2/type/10/",},{"name":"ice","url":"https://pokeapi.co/api/v2/type/15/",},],"double_damage_to":[{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"rock","url":"https://pokeapi.co/api/v2/type/6/",},{"name":"water","url":"https://pokeapi.co/api/v2/type/11/",},],"half_damage_from":[{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"water","url":"https://pokeapi.co/api/v2/type/11/",},{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"electric","url":"https://pokeapi.co/api/v2/type/13/",},],"half_damage_to":[{"name":"flying","url":"https://pokeapi.co/api/v2/type/3/",},{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"bug","url":"https://pokeapi.co/api/v2/type/7/",},{"name":"steel","url":"https://pokeapi.co/api/v2/type/9/",},{"name":"fire","url":"https://pokeapi.co/api/v2/type/10/",},{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"dragon","url":"https://pokeapi.co/api/v2/type/16/",},],"name":"grass","no_damage_from":[],"no_damage_to":[],},{"double_damage_from":[{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"psychic","url":"https://pokeapi.co/api/v2/type/14/",},],"double_damage_to":[{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"fairy","url":"https://pokeapi.co/api/v2/type/18/",},],"half_damage_from":[{"name":"fighting","url":"https://pokeapi.co/api/v2/type/2/",},{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"bug","url":"https://pokeapi.co/api/v2/type/7/",},{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"fairy","url":"https://pokeapi.co/api/v2/type/18/",},],"half_damage_to":[{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"rock","url":"https://pokeapi.co/api/v2/type/6/",},{"name":"ghost","url":"https://pokeapi.co/api/v2/type/8/",},],"name":"poison","no_damage_from":[],"no_damage_to":[{"name":"steel","url":"https://pokeapi.co/api/v2/type/9/",},],},],
result = src.reduce((acc, o) => {
Object.keys(o).forEach(key => {
if(Array.isArray(o[key])){
const match = acc.get(key),
items = o[key].map(({name}) => name)
match ?
match.push(...items) :
acc.set(key, items)
}
})
return acc
}, new Map)
console.log(result)
Does this work out?
I didn't know what to call each subelement from the main values array so thing is what i used. But this just digs into there and pulls out the name from each of the sublevel elements and tallies them up.
I didn't worry about uniqueness but you could (after concat) add a uniq clean up thing.
// where `values` is your initial data structure
const damageMap = values.reduce((memo, thing) => {
Object.keys(thing).forEach(key => {
if (key !== 'name') {
memo[key] = memo[key] || []
memo[key] = memo[key].concat(
thing[key].map(({name}) => name))
}
})
return memo
},{});
Using Array.reduce for collecting the data, Array#forEach to iterate over the elements and Object.entries to get key/values from the object.
let map = arr.reduce((acc, cur) => {
Object.entries(cur).forEach(([key, values]) => {
if (!acc[key] && key !=='name') {
acc[key] = [];
}
if (typeof(values)=== 'object') {
values.forEach(({name}) => {
acc[key].push(name);
});
}
});
return acc;
}, {});
Here for playing arround https://jsfiddle.net/h8kL5gp9/ or try this (with the hole code):
let arr = [
{
"double_damage_from": [
{
"name": "flying",
"url": "https://pokeapi.co/api/v2/type/3/",
},
{
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
{
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/",
},
{
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/",
},
{
"name": "ice",
"url": "https://pokeapi.co/api/v2/type/15/",
},
],
"double_damage_to": [
{
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
{
"name": "rock",
"url": "https://pokeapi.co/api/v2/type/6/",
},
{
"name": "water",
"url": "https://pokeapi.co/api/v2/type/11/",
},
],
"half_damage_from": [
{
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
{
"name": "water",
"url": "https://pokeapi.co/api/v2/type/11/",
},
{
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
{
"name": "electric",
"url": "https://pokeapi.co/api/v2/type/13/",
},
],
"half_damage_to": [
{
"name": "flying",
"url": "https://pokeapi.co/api/v2/type/3/",
},
{
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
{
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/",
},
{
"name": "steel",
"url": "https://pokeapi.co/api/v2/type/9/",
},
{
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/",
},
{
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
{
"name": "dragon",
"url": "https://pokeapi.co/api/v2/type/16/",
},
],
"name": "grass",
"no_damage_from": [],
"no_damage_to": [],
},
{
"double_damage_from": [
{
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
{
"name": "psychic",
"url": "https://pokeapi.co/api/v2/type/14/",
},
],
"double_damage_to": [
{
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
{
"name": "fairy",
"url": "https://pokeapi.co/api/v2/type/18/",
},
],
"half_damage_from": [
{
"name": "fighting",
"url": "https://pokeapi.co/api/v2/type/2/",
},
{
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
{
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/",
},
{
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/",
},
{
"name": "fairy",
"url": "https://pokeapi.co/api/v2/type/18/",
},
],
"half_damage_to": [
{
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/",
},
{
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/",
},
{
"name": "rock",
"url": "https://pokeapi.co/api/v2/type/6/",
},
{
"name": "ghost",
"url": "https://pokeapi.co/api/v2/type/8/",
},
],
"name": "poison",
"no_damage_from": [],
"no_damage_to": [
{
"name": "steel",
"url": "https://pokeapi.co/api/v2/type/9/",
},
],
},
];
let map = arr.reduce((acc, cur) => {
Object.entries(cur).forEach(([key, values]) => {
if (!acc[key] && key !=='name') {
acc[key] = [];
}
if (typeof(values)=== 'object') {
values.forEach(({name}) => {
acc[key].push(name);
});
}
});
return acc;
}, {});
console.log(map);