How to create array of object in javascript explicitly - javascript

I receive a file path string like so:
let sampletext="Home/Student/Hello.txt";
And I want to convert this into the following structure dynamically. How should I best go about this?
let idarray=[
{
'id':'Home',
'parent': '',
},
{
'id':'Student',
'parent': 'Home',
},
{
'id':'Hello.txt',
'parent': 'Student',
}
]

split on / and then build it from there - you can do this concisely with map:
let sampletext="Home/Student/Hello.txt";
let componentsArr = sampletext.split("/");
let idarray = componentsArr.map(( id, i ) => ({
id,
parent: componentsArr[i - 1] || ""
}));
console.log(idarray);
This is very simple - just set the id property to be whatever value you're currently iterating over, and then if there exists a value before it (i.e. if this is not the first/root item) then set its parent value to the previous value; otherwise, it's an empty string. You could remove componentsArr and refer to the splitting n times but that's mildly inefficient in my opinion.

Here you go:
let sampletext="Home/Student/Hello.txt"
let textarr = sampletext.split('/');
let idarray = textarr.map((x, i) => {
if(i === 0)
return { id: x, parent: '' };
return { id: x, parent: textarr[i - 1] };
});
console.log(idarray);

Related

Build array of taxonomies by filtering array by array of lowest-level taxonomies

There's a fairly big array of all available taxonomies in the shape of
{ id: string, parentId: string, label?: string }[], i.e.
temp1 = [
{ id: '1.1', parentId: '1' },
{ id: '1.1.1', parentId: '1.1' },
...
{ id: '1.9.8', parentId: '1.9' },
...
{ id: '1.9.8.15', parentId: '1.9.8' },
...etc
]
and a list of lowest-possible ID's that we should filter the array above by:
temp2 = ['1.9.8.19', '1.2.3.5', '1.9.8.15', ...etc]
The desired outcome is to have all the temp2 items collected from temp1 along with their parents, grandparents, grand-grandparents, etc, all up to the root, which is '1'.
My take on that was to create a recursive function:
const find = (ids) => ids.forEach(id => {
const found = temp1.find(item => item.id === id)
if (!found) return
c.push(found)
if (found.parentId === '1') return
find([found.parentId])
})
and then run it as follows: find(temp2).
The reason I don't like it is that:
I push to c array which lives outside of the scope of the function
I feel like I'm spawning multiple functions on find(temp2) but then I don't really use arrays anymore to do so: find([found.parentId]
I feel like the function is not performant
Data to play with:
temp2:
["1.9.8.19","1.9.8.3","1.8.1.2","1.8.1.3","1.2.3.5","1.2.3.1","1.9.8.15","1.2.4.1.1","1.9.8.5.1","1.9.8.11.1","1.9.8.21","1.2.3.10.1"]
temp1:
[{"id":"1.1","parentId":"1"},{"id":"1.1.1","parentId":"1.1"},{"id":"1.1.2","parentId":"1.1"},{"id":"1.1.3","parentId":"1.1"},{"id":"1.1.4","parentId":"1.1"},{"id":"1.1.5","parentId":"1.1"},{"id":"1.1.6","parentId":"1.1"},{"id":"1.1.7","parentId":"1.1"},{"id":"1.1.8","parentId":"1.1"},{"id":"1.10","parentId":"1"},{"id":"1.10.1","parentId":"1.10"},{"id":"1.10.10","parentId":"1.10"},{"id":"1.10.11","parentId":"1.10"},{"id":"1.10.12","parentId":"1.10"},{"id":"1.10.13","parentId":"1.10"},{"id":"1.10.14","parentId":"1.10"},{"id":"1.10.15","parentId":"1.10"},{"id":"1.10.16","parentId":"1.10"},{"id":"1.10.17","parentId":"1.10"},{"id":"1.10.18","parentId":"1.10"},{"id":"1.10.19","parentId":"1.10"},{"id":"1.10.2","parentId":"1.10"},{"id":"1.10.20","parentId":"1.10"},{"id":"1.10.21","parentId":"1.10"},{"id":"1.10.22","parentId":"1.10"},{"id":"1.10.23","parentId":"1.10"},{"id":"1.10.24","parentId":"1.10"},{"id":"1.10.25","parentId":"1.10"},{"id":"1.10.26","parentId":"1.10"},{"id":"1.10.3","parentId":"1.10"},{"id":"1.10.4","parentId":"1.10"},{"id":"1.10.5","parentId":"1.10"},{"id":"1.10.6","parentId":"1.10"},{"id":"1.10.7","parentId":"1.10"},{"id":"1.10.8","parentId":"1.10"},{"id":"1.10.9","parentId":"1.10"},{"id":"1.2","parentId":"1"},{"id":"1.2.1","parentId":"1.2"},{"id":"1.2.1.1","parentId":"1.2.1"},{"id":"1.2.1.10","parentId":"1.2.1"},{"id":"1.2.1.11","parentId":"1.2.1"},{"id":"1.2.1.12","parentId":"1.2.1"},{"id":"1.2.1.13","parentId":"1.2.1"},{"id":"1.2.1.14","parentId":"1.2.1"},{"id":"1.2.1.15","parentId":"1.2.1"},{"id":"1.2.1.16","parentId":"1.2.1"},{"id":"1.2.1.17","parentId":"1.2.1"},{"id":"1.2.1.18","parentId":"1.2.1"},{"id":"1.2.1.19","parentId":"1.2.1"},{"id":"1.2.1.2","parentId":"1.2.1"},{"id":"1.2.1.3","parentId":"1.2.1"},{"id":"1.2.1.4","parentId":"1.2.1"},{"id":"1.2.1.5","parentId":"1.2.1"},{"id":"1.2.1.6","parentId":"1.2.1"},{"id":"1.2.1.7","parentId":"1.2.1"},{"id":"1.2.1.8","parentId":"1.2.1"},{"id":"1.2.1.9","parentId":"1.2.1"},{"id":"1.2.2","parentId":"1.2"},{"id":"1.2.2.1","parentId":"1.2.2"},{"id":"1.2.2.2","parentId":"1.2.2"},{"id":"1.2.2.3","parentId":"1.2.2"},{"id":"1.2.2.4","parentId":"1.2.2"},{"id":"1.2.2.5","parentId":"1.2.2"},{"id":"1.2.2.6","parentId":"1.2.2"},{"id":"1.2.2.7","parentId":"1.2.2"},{"id":"1.2.3","parentId":"1.2"},{"id":"1.2.3.1","parentId":"1.2.3"},{"id":"1.2.3.10","parentId":"1.2.3"},{"id":"1.2.3.10.1","parentId":"1.2.3.10"},{"id":"1.2.3.10.2","parentId":"1.2.3.10"},{"id":"1.2.3.10.3","parentId":"1.2.3.10"},{"id":"1.2.3.11","parentId":"1.2.3"},{"id":"1.2.3.12","parentId":"1.2.3"},{"id":"1.2.3.12.1","parentId":"1.2.3.12"},{"id":"1.2.3.12.2","parentId":"1.2.3.12"},{"id":"1.2.3.12.3","parentId":"1.2.3.12"},{"id":"1.2.3.13","parentId":"1.2.3"},{"id":"1.2.3.14","parentId":"1.2.3"},{"id":"1.2.3.15","parentId":"1.2.3"},{"id":"1.2.3.16","parentId":"1.2.3"},{"id":"1.2.3.17","parentId":"1.2.3"},{"id":"1.2.3.18","parentId":"1.2.3"},{"id":"1.2.3.19","parentId":"1.2.3"},{"id":"1.2.3.2","parentId":"1.2.3"},{"id":"1.2.3.20","parentId":"1.2.3"},{"id":"1.2.3.21","parentId":"1.2.3"},{"id":"1.2.3.3","parentId":"1.2.3"},{"id":"1.2.3.4","parentId":"1.2.3"},{"id":"1.2.3.5","parentId":"1.2.3"},{"id":"1.2.3.6","parentId":"1.2.3"},{"id":"1.2.3.7","parentId":"1.2.3"},{"id":"1.2.3.8","parentId":"1.2.3"},{"id":"1.2.3.9","parentId":"1.2.3"},{"id":"1.2.4","parentId":"1.2"},{"id":"1.2.4.1","parentId":"1.2.4"},{"id":"1.2.4.1.1","parentId":"1.2.4.1"},{"id":"1.2.4.1.2","parentId":"1.2.4.1"},{"id":"1.2.4.1.3","parentId":"1.2.4.1"},{"id":"1.2.4.1.4","parentId":"1.2.4.1"},{"id":"1.2.4.2","parentId":"1.2.4"},{"id":"1.2.4.3","parentId":"1.2.4"},{"id":"1.2.4.3.1","parentId":"1.2.4.3"},{"id":"1.2.4.3.2","parentId":"1.2.4.3"},{"id":"1.2.4.3.3","parentId":"1.2.4.3"},{"id":"1.2.4.3.4","parentId":"1.2.4.3"},{"id":"1.2.4.3.5","parentId":"1.2.4.3"},{"id":"1.2.4.4","parentId":"1.2.4"},{"id":"1.3","parentId":"1"},{"id":"1.3.1","parentId":"1.3"},{"id":"1.3.2","parentId":"1.3"},{"id":"1.3.3","parentId":"1.3"},{"id":"1.3.4","parentId":"1.3"},{"id":"1.3.5","parentId":"1.3"},{"id":"1.3.6","parentId":"1.3"},{"id":"1.3.7","parentId":"1.3"},{"id":"1.3.8","parentId":"1.3"},{"id":"1.3.9","parentId":"1.3"},{"id":"1.4","parentId":"1"},{"id":"1.4.1","parentId":"1.4"},{"id":"1.4.2","parentId":"1.4"},{"id":"1.4.3","parentId":"1.4"},{"id":"1.5","parentId":"1"},{"id":"1.5.1","parentId":"1.5"},{"id":"1.5.2","parentId":"1.5"},{"id":"1.5.2.1","parentId":"1.5.2"},{"id":"1.5.2.2","parentId":"1.5.2"},{"id":"1.5.2.3","parentId":"1.5.2"},{"id":"1.5.2.4","parentId":"1.5.2"},{"id":"1.5.2.5","parentId":"1.5.2"},{"id":"1.5.2.6","parentId":"1.5.2"},{"id":"1.5.2.7","parentId":"1.5.2"},{"id":"1.5.2.8","parentId":"1.5.2"},{"id":"1.5.2.9","parentId":"1.5.2"},{"id":"1.5.3","parentId":"1.5"},{"id":"1.5.3.1","parentId":"1.5.3"},{"id":"1.5.3.2","parentId":"1.5.3"},{"id":"1.5.3.2.1","parentId":"1.5.3.2"},{"id":"1.5.3.2.2","parentId":"1.5.3.2"},{"id":"1.5.3.2.3","parentId":"1.5.3.2"},{"id":"1.5.3.3","parentId":"1.5.3"},{"id":"1.5.3.3.1","parentId":"1.5.3.3"},{"id":"1.5.3.3.2","parentId":"1.5.3.3"},{"id":"1.5.3.3.3","parentId":"1.5.3.3"},{"id":"1.5.3.4","parentId":"1.5.3"},{"id":"1.5.4","parentId":"1.5"},{"id":"1.5.4.1","parentId":"1.5.4"},{"id":"1.5.4.2","parentId":"1.5.4"},{"id":"1.5.4.3","parentId":"1.5.4"},{"id":"1.5.4.4","parentId":"1.5.4"},{"id":"1.5.4.5","parentId":"1.5.4"},{"id":"1.5.4.6","parentId":"1.5.4"},{"id":"1.5.4.7","parentId":"1.5.4"},{"id":"1.5.4.8","parentId":"1.5.4"},{"id":"1.6","parentId":"1"},{"id":"1.6.1","parentId":"1.6"},{"id":"1.6.2","parentId":"1.6"},{"id":"1.6.3","parentId":"1.6"},{"id":"1.6.4","parentId":"1.6"},{"id":"1.6.5","parentId":"1.6"},{"id":"1.6.6","parentId":"1.6"},{"id":"1.6.7","parentId":"1.6"},{"id":"1.6.8","parentId":"1.6"},{"id":"1.7","parentId":"1"},{"id":"1.7.1","parentId":"1.7"},{"id":"1.7.10","parentId":"1.7"},{"id":"1.7.11","parentId":"1.7"},{"id":"1.7.12","parentId":"1.7"},{"id":"1.7.13","parentId":"1.7"},{"id":"1.7.14","parentId":"1.7"},{"id":"1.7.15","parentId":"1.7"},{"id":"1.7.16","parentId":"1.7"},{"id":"1.7.2","parentId":"1.7"},{"id":"1.7.3","parentId":"1.7"},{"id":"1.7.4","parentId":"1.7"},{"id":"1.7.5","parentId":"1.7"},{"id":"1.7.6","parentId":"1.7"},{"id":"1.7.7","parentId":"1.7"},{"id":"1.7.8","parentId":"1.7"},{"id":"1.7.9","parentId":"1.7"},{"id":"1.8","parentId":"1"},{"id":"1.8.1","parentId":"1.8"},{"id":"1.8.1.1","parentId":"1.8.1"},{"id":"1.8.1.2","parentId":"1.8.1"},{"id":"1.8.1.3","parentId":"1.8.1"},{"id":"1.8.1.4","parentId":"1.8.1"},{"id":"1.8.1.5","parentId":"1.8.1"},{"id":"1.8.1.6","parentId":"1.8.1"},{"id":"1.8.1.7","parentId":"1.8.1"},{"id":"1.8.1.7.1","parentId":"1.8.1.7"},{"id":"1.8.1.7.2","parentId":"1.8.1.7"},{"id":"1.8.1.7.3","parentId":"1.8.1.7"},{"id":"1.8.2","parentId":"1.8"},{"id":"1.8.2.1","parentId":"1.8.2"},{"id":"1.8.2.2","parentId":"1.8.2"},{"id":"1.8.2.2.1","parentId":"1.8.2.2"},{"id":"1.8.2.2.2","parentId":"1.8.2.2"},{"id":"1.8.2.2.3","parentId":"1.8.2.2"},{"id":"1.8.2.3","parentId":"1.8.2"},{"id":"1.9","parentId":"1"},{"id":"1.9.1","parentId":"1.9"},{"id":"1.9.1.1","parentId":"1.9.1"},{"id":"1.9.1.2","parentId":"1.9.1"},{"id":"1.9.1.3","parentId":"1.9.1"},{"id":"1.9.1.4","parentId":"1.9.1"},{"id":"1.9.1.5","parentId":"1.9.1"},{"id":"1.9.1.6","parentId":"1.9.1"},{"id":"1.9.1.7","parentId":"1.9.1"},{"id":"1.9.1.8","parentId":"1.9.1"},{"id":"1.9.1.9","parentId":"1.9.1"},{"id":"1.9.10","parentId":"1.9"},{"id":"1.9.10.1","parentId":"1.9.10"},{"id":"1.9.10.2","parentId":"1.9.10"},{"id":"1.9.10.3","parentId":"1.9.10"},{"id":"1.9.10.4","parentId":"1.9.10"},{"id":"1.9.10.5","parentId":"1.9.10"},{"id":"1.9.11","parentId":"1.9"},{"id":"1.9.11.1","parentId":"1.9.11"},{"id":"1.9.11.2","parentId":"1.9.11"},{"id":"1.9.11.3","parentId":"1.9.11"},{"id":"1.9.11.4","parentId":"1.9.11"},{"id":"1.9.11.5","parentId":"1.9.11"},{"id":"1.9.11.6","parentId":"1.9.11"},{"id":"1.9.11.7","parentId":"1.9.11"},{"id":"1.9.12","parentId":"1.9"},{"id":"1.9.13","parentId":"1.9"},{"id":"1.9.2","parentId":"1.9"},{"id":"1.9.2.1","parentId":"1.9.2"},{"id":"1.9.2.2","parentId":"1.9.2"},{"id":"1.9.2.3","parentId":"1.9.2"},{"id":"1.9.2.4","parentId":"1.9.2"},{"id":"1.9.2.5","parentId":"1.9.2"},{"id":"1.9.2.6","parentId":"1.9.2"},{"id":"1.9.3","parentId":"1.9"},{"id":"1.9.4","parentId":"1.9"},{"id":"1.9.5","parentId":"1.9"},{"id":"1.9.6","parentId":"1.9"},{"id":"1.9.7","parentId":"1.9"},{"id":"1.9.7.1","parentId":"1.9.7"},{"id":"1.9.7.1.1","parentId":"1.9.7.1"},{"id":"1.9.7.1.2","parentId":"1.9.7.1"},{"id":"1.9.7.1.3","parentId":"1.9.7.1"},{"id":"1.9.7.1.4","parentId":"1.9.7.1"},{"id":"1.9.7.1.5","parentId":"1.9.7.1"},{"id":"1.9.7.2","parentId":"1.9.7"},{"id":"1.9.7.2.1","parentId":"1.9.7.2"},{"id":"1.9.7.2.2","parentId":"1.9.7.2"},{"id":"1.9.7.2.3","parentId":"1.9.7.2"},{"id":"1.9.7.2.4","parentId":"1.9.7.2"},{"id":"1.9.7.2.5","parentId":"1.9.7.2"},{"id":"1.9.7.3","parentId":"1.9.7"},{"id":"1.9.7.3.1","parentId":"1.9.7.3"},{"id":"1.9.7.3.2","parentId":"1.9.7.3"},{"id":"1.9.7.3.3","parentId":"1.9.7.3"},{"id":"1.9.7.3.4","parentId":"1.9.7.3"},{"id":"1.9.7.3.5","parentId":"1.9.7.3"},{"id":"1.9.7.4","parentId":"1.9.7"},{"id":"1.9.8","parentId":"1.9"},{"id":"1.9.8.1","parentId":"1.9.8"},{"id":"1.9.8.1.1","parentId":"1.9.8.1"},{"id":"1.9.8.1.2","parentId":"1.9.8.1"},{"id":"1.9.8.1.3","parentId":"1.9.8.1"},{"id":"1.9.8.1.4","parentId":"1.9.8.1"},{"id":"1.9.8.1.5","parentId":"1.9.8.1"},{"id":"1.9.8.10","parentId":"1.9.8"},{"id":"1.9.8.11","parentId":"1.9.8"},{"id":"1.9.8.11.1","parentId":"1.9.8.11"},{"id":"1.9.8.11.2","parentId":"1.9.8.11"},{"id":"1.9.8.11.3","parentId":"1.9.8.11"},{"id":"1.9.8.12","parentId":"1.9.8"},{"id":"1.9.8.13","parentId":"1.9.8"},{"id":"1.9.8.14","parentId":"1.9.8"},{"id":"1.9.8.15","parentId":"1.9.8"},{"id":"1.9.8.16","parentId":"1.9.8"},{"id":"1.9.8.17","parentId":"1.9.8"},{"id":"1.9.8.18","parentId":"1.9.8"},{"id":"1.9.8.19","parentId":"1.9.8"},{"id":"1.9.8.2","parentId":"1.9.8"},{"id":"1.9.8.20","parentId":"1.9.8"},{"id":"1.9.8.21","parentId":"1.9.8"},{"id":"1.9.8.22","parentId":"1.9.8"},{"id":"1.9.8.23","parentId":"1.9.8"},{"id":"1.9.8.24","parentId":"1.9.8"},{"id":"1.9.8.25","parentId":"1.9.8"},{"id":"1.9.8.25.1","parentId":"1.9.8.25"},{"id":"1.9.8.25.2","parentId":"1.9.8.25"},{"id":"1.9.8.25.3","parentId":"1.9.8.25"},{"id":"1.9.8.26","parentId":"1.9.8"},{"id":"1.9.8.27","parentId":"1.9.8"},{"id":"1.9.8.27.1","parentId":"1.9.8.27"},{"id":"1.9.8.27.2","parentId":"1.9.8.27"},{"id":"1.9.8.3","parentId":"1.9.8"},{"id":"1.9.8.4","parentId":"1.9.8"},{"id":"1.9.8.5","parentId":"1.9.8"},{"id":"1.9.8.5.1","parentId":"1.9.8.5"},{"id":"1.9.8.5.2","parentId":"1.9.8.5"},{"id":"1.9.8.5.3","parentId":"1.9.8.5"},{"id":"1.9.8.6","parentId":"1.9.8"},{"id":"1.9.8.6.1","parentId":"1.9.8.6"},{"id":"1.9.8.6.2","parentId":"1.9.8.6"},{"id":"1.9.8.7","parentId":"1.9.8"},{"id":"1.9.8.8","parentId":"1.9.8"},{"id":"1.9.8.8.1","parentId":"1.9.8.8"},{"id":"1.9.8.8.2","parentId":"1.9.8.8"},{"id":"1.9.8.9","parentId":"1.9.8"},{"id":"1.9.9","parentId":"1.9"},{"id":"1.9.9.1","parentId":"1.9.9"},{"id":"1.9.9.10","parentId":"1.9.9"},{"id":"1.9.9.11","parentId":"1.9.9"},{"id":"1.9.9.12","parentId":"1.9.9"},{"id":"1.9.9.13","parentId":"1.9.9"},{"id":"1.9.9.2","parentId":"1.9.9"},{"id":"1.9.9.3","parentId":"1.9.9"},{"id":"1.9.9.3.1","parentId":"1.9.9.3"},{"id":"1.9.9.3.10","parentId":"1.9.9.3"},{"id":"1.9.9.3.2","parentId":"1.9.9.3"},{"id":"1.9.9.3.3","parentId":"1.9.9.3"},{"id":"1.9.9.3.4","parentId":"1.9.9.3"},{"id":"1.9.9.3.5","parentId":"1.9.9.3"},{"id":"1.9.9.3.6","parentId":"1.9.9.3"},{"id":"1.9.9.3.7","parentId":"1.9.9.3"},{"id":"1.9.9.3.8","parentId":"1.9.9.3"},{"id":"1.9.9.3.9","parentId":"1.9.9.3"},{"id":"1.9.9.4","parentId":"1.9.9"},{"id":"1.9.9.5","parentId":"1.9.9"},{"id":"1.9.9.6","parentId":"1.9.9"},{"id":"1.9.9.7","parentId":"1.9.9"},{"id":"1.9.9.8","parentId":"1.9.9"},{"id":"1.9.9.9","parentId":"1.9.9"}]
Note: temp2 values are guaranteed to be the lowest possible taxonomy levels
const a = [{"id":"1.1","parentId":"1"},{"id":"1.1.1","parentId":"1.1"},{"id":"1.1.2","parentId":"1.1"},{"id":"1.1.3","parentId":"1.1"},{"id":"1.1.4","parentId":"1.1"},{"id":"1.1.5","parentId":"1.1"},{"id":"1.1.6","parentId":"1.1"},{"id":"1.1.7","parentId":"1.1"},{"id":"1.1.8","parentId":"1.1"},{"id":"1.10","parentId":"1"},{"id":"1.10.1","parentId":"1.10"},{"id":"1.10.10","parentId":"1.10"},{"id":"1.10.11","parentId":"1.10"},{"id":"1.10.12","parentId":"1.10"},{"id":"1.10.13","parentId":"1.10"},{"id":"1.10.14","parentId":"1.10"},{"id":"1.10.15","parentId":"1.10"},{"id":"1.10.16","parentId":"1.10"},{"id":"1.10.17","parentId":"1.10"},{"id":"1.10.18","parentId":"1.10"},{"id":"1.10.19","parentId":"1.10"},{"id":"1.10.2","parentId":"1.10"},{"id":"1.10.20","parentId":"1.10"},{"id":"1.10.21","parentId":"1.10"},{"id":"1.10.22","parentId":"1.10"},{"id":"1.10.23","parentId":"1.10"},{"id":"1.10.24","parentId":"1.10"},{"id":"1.10.25","parentId":"1.10"},{"id":"1.10.26","parentId":"1.10"},{"id":"1.10.3","parentId":"1.10"},{"id":"1.10.4","parentId":"1.10"},{"id":"1.10.5","parentId":"1.10"},{"id":"1.10.6","parentId":"1.10"},{"id":"1.10.7","parentId":"1.10"},{"id":"1.10.8","parentId":"1.10"},{"id":"1.10.9","parentId":"1.10"},{"id":"1.2","parentId":"1"},{"id":"1.2.1","parentId":"1.2"},{"id":"1.2.1.1","parentId":"1.2.1"},{"id":"1.2.1.10","parentId":"1.2.1"},{"id":"1.2.1.11","parentId":"1.2.1"},{"id":"1.2.1.12","parentId":"1.2.1"},{"id":"1.2.1.13","parentId":"1.2.1"},{"id":"1.2.1.14","parentId":"1.2.1"},{"id":"1.2.1.15","parentId":"1.2.1"},{"id":"1.2.1.16","parentId":"1.2.1"},{"id":"1.2.1.17","parentId":"1.2.1"},{"id":"1.2.1.18","parentId":"1.2.1"},{"id":"1.2.1.19","parentId":"1.2.1"},{"id":"1.2.1.2","parentId":"1.2.1"},{"id":"1.2.1.3","parentId":"1.2.1"},{"id":"1.2.1.4","parentId":"1.2.1"},{"id":"1.2.1.5","parentId":"1.2.1"},{"id":"1.2.1.6","parentId":"1.2.1"},{"id":"1.2.1.7","parentId":"1.2.1"},{"id":"1.2.1.8","parentId":"1.2.1"},{"id":"1.2.1.9","parentId":"1.2.1"},{"id":"1.2.2","parentId":"1.2"},{"id":"1.2.2.1","parentId":"1.2.2"},{"id":"1.2.2.2","parentId":"1.2.2"},{"id":"1.2.2.3","parentId":"1.2.2"},{"id":"1.2.2.4","parentId":"1.2.2"},{"id":"1.2.2.5","parentId":"1.2.2"},{"id":"1.2.2.6","parentId":"1.2.2"},{"id":"1.2.2.7","parentId":"1.2.2"},{"id":"1.2.3","parentId":"1.2"},{"id":"1.2.3.1","parentId":"1.2.3"},{"id":"1.2.3.10","parentId":"1.2.3"},{"id":"1.2.3.10.1","parentId":"1.2.3.10"},{"id":"1.2.3.10.2","parentId":"1.2.3.10"},{"id":"1.2.3.10.3","parentId":"1.2.3.10"},{"id":"1.2.3.11","parentId":"1.2.3"},{"id":"1.2.3.12","parentId":"1.2.3"},{"id":"1.2.3.12.1","parentId":"1.2.3.12"},{"id":"1.2.3.12.2","parentId":"1.2.3.12"},{"id":"1.2.3.12.3","parentId":"1.2.3.12"},{"id":"1.2.3.13","parentId":"1.2.3"},{"id":"1.2.3.14","parentId":"1.2.3"},{"id":"1.2.3.15","parentId":"1.2.3"},{"id":"1.2.3.16","parentId":"1.2.3"},{"id":"1.2.3.17","parentId":"1.2.3"},{"id":"1.2.3.18","parentId":"1.2.3"},{"id":"1.2.3.19","parentId":"1.2.3"},{"id":"1.2.3.2","parentId":"1.2.3"},{"id":"1.2.3.20","parentId":"1.2.3"},{"id":"1.2.3.21","parentId":"1.2.3"},{"id":"1.2.3.3","parentId":"1.2.3"},{"id":"1.2.3.4","parentId":"1.2.3"},{"id":"1.2.3.5","parentId":"1.2.3"},{"id":"1.2.3.6","parentId":"1.2.3"},{"id":"1.2.3.7","parentId":"1.2.3"},{"id":"1.2.3.8","parentId":"1.2.3"},{"id":"1.2.3.9","parentId":"1.2.3"},{"id":"1.2.4","parentId":"1.2"},{"id":"1.2.4.1","parentId":"1.2.4"},{"id":"1.2.4.1.1","parentId":"1.2.4.1"},{"id":"1.2.4.1.2","parentId":"1.2.4.1"},{"id":"1.2.4.1.3","parentId":"1.2.4.1"},{"id":"1.2.4.1.4","parentId":"1.2.4.1"},{"id":"1.2.4.2","parentId":"1.2.4"},{"id":"1.2.4.3","parentId":"1.2.4"},{"id":"1.2.4.3.1","parentId":"1.2.4.3"},{"id":"1.2.4.3.2","parentId":"1.2.4.3"},{"id":"1.2.4.3.3","parentId":"1.2.4.3"},{"id":"1.2.4.3.4","parentId":"1.2.4.3"},{"id":"1.2.4.3.5","parentId":"1.2.4.3"},{"id":"1.2.4.4","parentId":"1.2.4"},{"id":"1.3","parentId":"1"},{"id":"1.3.1","parentId":"1.3"},{"id":"1.3.2","parentId":"1.3"},{"id":"1.3.3","parentId":"1.3"},{"id":"1.3.4","parentId":"1.3"},{"id":"1.3.5","parentId":"1.3"},{"id":"1.3.6","parentId":"1.3"},{"id":"1.3.7","parentId":"1.3"},{"id":"1.3.8","parentId":"1.3"},{"id":"1.3.9","parentId":"1.3"},{"id":"1.4","parentId":"1"},{"id":"1.4.1","parentId":"1.4"},{"id":"1.4.2","parentId":"1.4"},{"id":"1.4.3","parentId":"1.4"},{"id":"1.5","parentId":"1"},{"id":"1.5.1","parentId":"1.5"},{"id":"1.5.2","parentId":"1.5"},{"id":"1.5.2.1","parentId":"1.5.2"},{"id":"1.5.2.2","parentId":"1.5.2"},{"id":"1.5.2.3","parentId":"1.5.2"},{"id":"1.5.2.4","parentId":"1.5.2"},{"id":"1.5.2.5","parentId":"1.5.2"},{"id":"1.5.2.6","parentId":"1.5.2"},{"id":"1.5.2.7","parentId":"1.5.2"},{"id":"1.5.2.8","parentId":"1.5.2"},{"id":"1.5.2.9","parentId":"1.5.2"},{"id":"1.5.3","parentId":"1.5"},{"id":"1.5.3.1","parentId":"1.5.3"},{"id":"1.5.3.2","parentId":"1.5.3"},{"id":"1.5.3.2.1","parentId":"1.5.3.2"},{"id":"1.5.3.2.2","parentId":"1.5.3.2"},{"id":"1.5.3.2.3","parentId":"1.5.3.2"},{"id":"1.5.3.3","parentId":"1.5.3"},{"id":"1.5.3.3.1","parentId":"1.5.3.3"},{"id":"1.5.3.3.2","parentId":"1.5.3.3"},{"id":"1.5.3.3.3","parentId":"1.5.3.3"},{"id":"1.5.3.4","parentId":"1.5.3"},{"id":"1.5.4","parentId":"1.5"},{"id":"1.5.4.1","parentId":"1.5.4"},{"id":"1.5.4.2","parentId":"1.5.4"},{"id":"1.5.4.3","parentId":"1.5.4"},{"id":"1.5.4.4","parentId":"1.5.4"},{"id":"1.5.4.5","parentId":"1.5.4"},{"id":"1.5.4.6","parentId":"1.5.4"},{"id":"1.5.4.7","parentId":"1.5.4"},{"id":"1.5.4.8","parentId":"1.5.4"},{"id":"1.6","parentId":"1"},{"id":"1.6.1","parentId":"1.6"},{"id":"1.6.2","parentId":"1.6"},{"id":"1.6.3","parentId":"1.6"},{"id":"1.6.4","parentId":"1.6"},{"id":"1.6.5","parentId":"1.6"},{"id":"1.6.6","parentId":"1.6"},{"id":"1.6.7","parentId":"1.6"},{"id":"1.6.8","parentId":"1.6"},{"id":"1.7","parentId":"1"},{"id":"1.7.1","parentId":"1.7"},{"id":"1.7.10","parentId":"1.7"},{"id":"1.7.11","parentId":"1.7"},{"id":"1.7.12","parentId":"1.7"},{"id":"1.7.13","parentId":"1.7"},{"id":"1.7.14","parentId":"1.7"},{"id":"1.7.15","parentId":"1.7"},{"id":"1.7.16","parentId":"1.7"},{"id":"1.7.2","parentId":"1.7"},{"id":"1.7.3","parentId":"1.7"},{"id":"1.7.4","parentId":"1.7"},{"id":"1.7.5","parentId":"1.7"},{"id":"1.7.6","parentId":"1.7"},{"id":"1.7.7","parentId":"1.7"},{"id":"1.7.8","parentId":"1.7"},{"id":"1.7.9","parentId":"1.7"},{"id":"1.8","parentId":"1"},{"id":"1.8.1","parentId":"1.8"},{"id":"1.8.1.1","parentId":"1.8.1"},{"id":"1.8.1.2","parentId":"1.8.1"},{"id":"1.8.1.3","parentId":"1.8.1"},{"id":"1.8.1.4","parentId":"1.8.1"},{"id":"1.8.1.5","parentId":"1.8.1"},{"id":"1.8.1.6","parentId":"1.8.1"},{"id":"1.8.1.7","parentId":"1.8.1"},{"id":"1.8.1.7.1","parentId":"1.8.1.7"},{"id":"1.8.1.7.2","parentId":"1.8.1.7"},{"id":"1.8.1.7.3","parentId":"1.8.1.7"},{"id":"1.8.2","parentId":"1.8"},{"id":"1.8.2.1","parentId":"1.8.2"},{"id":"1.8.2.2","parentId":"1.8.2"},{"id":"1.8.2.2.1","parentId":"1.8.2.2"},{"id":"1.8.2.2.2","parentId":"1.8.2.2"},{"id":"1.8.2.2.3","parentId":"1.8.2.2"},{"id":"1.8.2.3","parentId":"1.8.2"},{"id":"1.9","parentId":"1"},{"id":"1.9.1","parentId":"1.9"},{"id":"1.9.1.1","parentId":"1.9.1"},{"id":"1.9.1.2","parentId":"1.9.1"},{"id":"1.9.1.3","parentId":"1.9.1"},{"id":"1.9.1.4","parentId":"1.9.1"},{"id":"1.9.1.5","parentId":"1.9.1"},{"id":"1.9.1.6","parentId":"1.9.1"},{"id":"1.9.1.7","parentId":"1.9.1"},{"id":"1.9.1.8","parentId":"1.9.1"},{"id":"1.9.1.9","parentId":"1.9.1"},{"id":"1.9.10","parentId":"1.9"},{"id":"1.9.10.1","parentId":"1.9.10"},{"id":"1.9.10.2","parentId":"1.9.10"},{"id":"1.9.10.3","parentId":"1.9.10"},{"id":"1.9.10.4","parentId":"1.9.10"},{"id":"1.9.10.5","parentId":"1.9.10"},{"id":"1.9.11","parentId":"1.9"},{"id":"1.9.11.1","parentId":"1.9.11"},{"id":"1.9.11.2","parentId":"1.9.11"},{"id":"1.9.11.3","parentId":"1.9.11"},{"id":"1.9.11.4","parentId":"1.9.11"},{"id":"1.9.11.5","parentId":"1.9.11"},{"id":"1.9.11.6","parentId":"1.9.11"},{"id":"1.9.11.7","parentId":"1.9.11"},{"id":"1.9.12","parentId":"1.9"},{"id":"1.9.13","parentId":"1.9"},{"id":"1.9.2","parentId":"1.9"},{"id":"1.9.2.1","parentId":"1.9.2"},{"id":"1.9.2.2","parentId":"1.9.2"},{"id":"1.9.2.3","parentId":"1.9.2"},{"id":"1.9.2.4","parentId":"1.9.2"},{"id":"1.9.2.5","parentId":"1.9.2"},{"id":"1.9.2.6","parentId":"1.9.2"},{"id":"1.9.3","parentId":"1.9"},{"id":"1.9.4","parentId":"1.9"},{"id":"1.9.5","parentId":"1.9"},{"id":"1.9.6","parentId":"1.9"},{"id":"1.9.7","parentId":"1.9"},{"id":"1.9.7.1","parentId":"1.9.7"},{"id":"1.9.7.1.1","parentId":"1.9.7.1"},{"id":"1.9.7.1.2","parentId":"1.9.7.1"},{"id":"1.9.7.1.3","parentId":"1.9.7.1"},{"id":"1.9.7.1.4","parentId":"1.9.7.1"},{"id":"1.9.7.1.5","parentId":"1.9.7.1"},{"id":"1.9.7.2","parentId":"1.9.7"},{"id":"1.9.7.2.1","parentId":"1.9.7.2"},{"id":"1.9.7.2.2","parentId":"1.9.7.2"},{"id":"1.9.7.2.3","parentId":"1.9.7.2"},{"id":"1.9.7.2.4","parentId":"1.9.7.2"},{"id":"1.9.7.2.5","parentId":"1.9.7.2"},{"id":"1.9.7.3","parentId":"1.9.7"},{"id":"1.9.7.3.1","parentId":"1.9.7.3"},{"id":"1.9.7.3.2","parentId":"1.9.7.3"},{"id":"1.9.7.3.3","parentId":"1.9.7.3"},{"id":"1.9.7.3.4","parentId":"1.9.7.3"},{"id":"1.9.7.3.5","parentId":"1.9.7.3"},{"id":"1.9.7.4","parentId":"1.9.7"},{"id":"1.9.8","parentId":"1.9"},{"id":"1.9.8.1","parentId":"1.9.8"},{"id":"1.9.8.1.1","parentId":"1.9.8.1"},{"id":"1.9.8.1.2","parentId":"1.9.8.1"},{"id":"1.9.8.1.3","parentId":"1.9.8.1"},{"id":"1.9.8.1.4","parentId":"1.9.8.1"},{"id":"1.9.8.1.5","parentId":"1.9.8.1"},{"id":"1.9.8.10","parentId":"1.9.8"},{"id":"1.9.8.11","parentId":"1.9.8"},{"id":"1.9.8.11.1","parentId":"1.9.8.11"},{"id":"1.9.8.11.2","parentId":"1.9.8.11"},{"id":"1.9.8.11.3","parentId":"1.9.8.11"},{"id":"1.9.8.12","parentId":"1.9.8"},{"id":"1.9.8.13","parentId":"1.9.8"},{"id":"1.9.8.14","parentId":"1.9.8"},{"id":"1.9.8.15","parentId":"1.9.8"},{"id":"1.9.8.16","parentId":"1.9.8"},{"id":"1.9.8.17","parentId":"1.9.8"},{"id":"1.9.8.18","parentId":"1.9.8"},{"id":"1.9.8.19","parentId":"1.9.8"},{"id":"1.9.8.2","parentId":"1.9.8"},{"id":"1.9.8.20","parentId":"1.9.8"},{"id":"1.9.8.21","parentId":"1.9.8"},{"id":"1.9.8.22","parentId":"1.9.8"},{"id":"1.9.8.23","parentId":"1.9.8"},{"id":"1.9.8.24","parentId":"1.9.8"},{"id":"1.9.8.25","parentId":"1.9.8"},{"id":"1.9.8.25.1","parentId":"1.9.8.25"},{"id":"1.9.8.25.2","parentId":"1.9.8.25"},{"id":"1.9.8.25.3","parentId":"1.9.8.25"},{"id":"1.9.8.26","parentId":"1.9.8"},{"id":"1.9.8.27","parentId":"1.9.8"},{"id":"1.9.8.27.1","parentId":"1.9.8.27"},{"id":"1.9.8.27.2","parentId":"1.9.8.27"},{"id":"1.9.8.3","parentId":"1.9.8"},{"id":"1.9.8.4","parentId":"1.9.8"},{"id":"1.9.8.5","parentId":"1.9.8"},{"id":"1.9.8.5.1","parentId":"1.9.8.5"},{"id":"1.9.8.5.2","parentId":"1.9.8.5"},{"id":"1.9.8.5.3","parentId":"1.9.8.5"},{"id":"1.9.8.6","parentId":"1.9.8"},{"id":"1.9.8.6.1","parentId":"1.9.8.6"},{"id":"1.9.8.6.2","parentId":"1.9.8.6"},{"id":"1.9.8.7","parentId":"1.9.8"},{"id":"1.9.8.8","parentId":"1.9.8"},{"id":"1.9.8.8.1","parentId":"1.9.8.8"},{"id":"1.9.8.8.2","parentId":"1.9.8.8"},{"id":"1.9.8.9","parentId":"1.9.8"},{"id":"1.9.9","parentId":"1.9"},{"id":"1.9.9.1","parentId":"1.9.9"},{"id":"1.9.9.10","parentId":"1.9.9"},{"id":"1.9.9.11","parentId":"1.9.9"},{"id":"1.9.9.12","parentId":"1.9.9"},{"id":"1.9.9.13","parentId":"1.9.9"},{"id":"1.9.9.2","parentId":"1.9.9"},{"id":"1.9.9.3","parentId":"1.9.9"},{"id":"1.9.9.3.1","parentId":"1.9.9.3"},{"id":"1.9.9.3.10","parentId":"1.9.9.3"},{"id":"1.9.9.3.2","parentId":"1.9.9.3"},{"id":"1.9.9.3.3","parentId":"1.9.9.3"},{"id":"1.9.9.3.4","parentId":"1.9.9.3"},{"id":"1.9.9.3.5","parentId":"1.9.9.3"},{"id":"1.9.9.3.6","parentId":"1.9.9.3"},{"id":"1.9.9.3.7","parentId":"1.9.9.3"},{"id":"1.9.9.3.8","parentId":"1.9.9.3"},{"id":"1.9.9.3.9","parentId":"1.9.9.3"},{"id":"1.9.9.4","parentId":"1.9.9"},{"id":"1.9.9.5","parentId":"1.9.9"},{"id":"1.9.9.6","parentId":"1.9.9"},{"id":"1.9.9.7","parentId":"1.9.9"},{"id":"1.9.9.8","parentId":"1.9.9"},{"id":"1.9.9.9","parentId":"1.9.9"}]
const b = ["1.9.8.19","1.9.8.3","1.8.1.2","1.8.1.3","1.2.3.5","1.2.3.1","1.9.8.15","1.2.4.1.1","1.9.8.5.1","1.9.8.11.1","1.9.8.21","1.2.3.10.1"]
const f = (a,b,c) => [...new Set([
...c=a.filter(({id})=>b.includes(id)),
...c.length?f(a,c.map(i=>i.parentId)):[]])]
console.log(f(a,b))
You can use Array#flatMap with a recursive function.
let temp2 = ["1.9.8.19","1.9.8.3","1.8.1.2","1.8.1.3","1.2.3.5","1.2.3.1","1.9.8.15","1.2.4.1.1","1.9.8.5.1","1.9.8.11.1","1.9.8.21","1.2.3.10.1"], temp1 = [{"id":"1.1","parentId":"1"},{"id":"1.1.1","parentId":"1.1"},{"id":"1.1.2","parentId":"1.1"},{"id":"1.1.3","parentId":"1.1"},{"id":"1.1.4","parentId":"1.1"},{"id":"1.1.5","parentId":"1.1"},{"id":"1.1.6","parentId":"1.1"},{"id":"1.1.7","parentId":"1.1"},{"id":"1.1.8","parentId":"1.1"},{"id":"1.10","parentId":"1"},{"id":"1.10.1","parentId":"1.10"},{"id":"1.10.10","parentId":"1.10"},{"id":"1.10.11","parentId":"1.10"},{"id":"1.10.12","parentId":"1.10"},{"id":"1.10.13","parentId":"1.10"},{"id":"1.10.14","parentId":"1.10"},{"id":"1.10.15","parentId":"1.10"},{"id":"1.10.16","parentId":"1.10"},{"id":"1.10.17","parentId":"1.10"},{"id":"1.10.18","parentId":"1.10"},{"id":"1.10.19","parentId":"1.10"},{"id":"1.10.2","parentId":"1.10"},{"id":"1.10.20","parentId":"1.10"},{"id":"1.10.21","parentId":"1.10"},{"id":"1.10.22","parentId":"1.10"},{"id":"1.10.23","parentId":"1.10"},{"id":"1.10.24","parentId":"1.10"},{"id":"1.10.25","parentId":"1.10"},{"id":"1.10.26","parentId":"1.10"},{"id":"1.10.3","parentId":"1.10"},{"id":"1.10.4","parentId":"1.10"},{"id":"1.10.5","parentId":"1.10"},{"id":"1.10.6","parentId":"1.10"},{"id":"1.10.7","parentId":"1.10"},{"id":"1.10.8","parentId":"1.10"},{"id":"1.10.9","parentId":"1.10"},{"id":"1.2","parentId":"1"},{"id":"1.2.1","parentId":"1.2"},{"id":"1.2.1.1","parentId":"1.2.1"},{"id":"1.2.1.10","parentId":"1.2.1"},{"id":"1.2.1.11","parentId":"1.2.1"},{"id":"1.2.1.12","parentId":"1.2.1"},{"id":"1.2.1.13","parentId":"1.2.1"},{"id":"1.2.1.14","parentId":"1.2.1"},{"id":"1.2.1.15","parentId":"1.2.1"},{"id":"1.2.1.16","parentId":"1.2.1"},{"id":"1.2.1.17","parentId":"1.2.1"},{"id":"1.2.1.18","parentId":"1.2.1"},{"id":"1.2.1.19","parentId":"1.2.1"},{"id":"1.2.1.2","parentId":"1.2.1"},{"id":"1.2.1.3","parentId":"1.2.1"},{"id":"1.2.1.4","parentId":"1.2.1"},{"id":"1.2.1.5","parentId":"1.2.1"},{"id":"1.2.1.6","parentId":"1.2.1"},{"id":"1.2.1.7","parentId":"1.2.1"},{"id":"1.2.1.8","parentId":"1.2.1"},{"id":"1.2.1.9","parentId":"1.2.1"},{"id":"1.2.2","parentId":"1.2"},{"id":"1.2.2.1","parentId":"1.2.2"},{"id":"1.2.2.2","parentId":"1.2.2"},{"id":"1.2.2.3","parentId":"1.2.2"},{"id":"1.2.2.4","parentId":"1.2.2"},{"id":"1.2.2.5","parentId":"1.2.2"},{"id":"1.2.2.6","parentId":"1.2.2"},{"id":"1.2.2.7","parentId":"1.2.2"},{"id":"1.2.3","parentId":"1.2"},{"id":"1.2.3.1","parentId":"1.2.3"},{"id":"1.2.3.10","parentId":"1.2.3"},{"id":"1.2.3.10.1","parentId":"1.2.3.10"},{"id":"1.2.3.10.2","parentId":"1.2.3.10"},{"id":"1.2.3.10.3","parentId":"1.2.3.10"},{"id":"1.2.3.11","parentId":"1.2.3"},{"id":"1.2.3.12","parentId":"1.2.3"},{"id":"1.2.3.12.1","parentId":"1.2.3.12"},{"id":"1.2.3.12.2","parentId":"1.2.3.12"},{"id":"1.2.3.12.3","parentId":"1.2.3.12"},{"id":"1.2.3.13","parentId":"1.2.3"},{"id":"1.2.3.14","parentId":"1.2.3"},{"id":"1.2.3.15","parentId":"1.2.3"},{"id":"1.2.3.16","parentId":"1.2.3"},{"id":"1.2.3.17","parentId":"1.2.3"},{"id":"1.2.3.18","parentId":"1.2.3"},{"id":"1.2.3.19","parentId":"1.2.3"},{"id":"1.2.3.2","parentId":"1.2.3"},{"id":"1.2.3.20","parentId":"1.2.3"},{"id":"1.2.3.21","parentId":"1.2.3"},{"id":"1.2.3.3","parentId":"1.2.3"},{"id":"1.2.3.4","parentId":"1.2.3"},{"id":"1.2.3.5","parentId":"1.2.3"},{"id":"1.2.3.6","parentId":"1.2.3"},{"id":"1.2.3.7","parentId":"1.2.3"},{"id":"1.2.3.8","parentId":"1.2.3"},{"id":"1.2.3.9","parentId":"1.2.3"},{"id":"1.2.4","parentId":"1.2"},{"id":"1.2.4.1","parentId":"1.2.4"},{"id":"1.2.4.1.1","parentId":"1.2.4.1"},{"id":"1.2.4.1.2","parentId":"1.2.4.1"},{"id":"1.2.4.1.3","parentId":"1.2.4.1"},{"id":"1.2.4.1.4","parentId":"1.2.4.1"},{"id":"1.2.4.2","parentId":"1.2.4"},{"id":"1.2.4.3","parentId":"1.2.4"},{"id":"1.2.4.3.1","parentId":"1.2.4.3"},{"id":"1.2.4.3.2","parentId":"1.2.4.3"},{"id":"1.2.4.3.3","parentId":"1.2.4.3"},{"id":"1.2.4.3.4","parentId":"1.2.4.3"},{"id":"1.2.4.3.5","parentId":"1.2.4.3"},{"id":"1.2.4.4","parentId":"1.2.4"},{"id":"1.3","parentId":"1"},{"id":"1.3.1","parentId":"1.3"},{"id":"1.3.2","parentId":"1.3"},{"id":"1.3.3","parentId":"1.3"},{"id":"1.3.4","parentId":"1.3"},{"id":"1.3.5","parentId":"1.3"},{"id":"1.3.6","parentId":"1.3"},{"id":"1.3.7","parentId":"1.3"},{"id":"1.3.8","parentId":"1.3"},{"id":"1.3.9","parentId":"1.3"},{"id":"1.4","parentId":"1"},{"id":"1.4.1","parentId":"1.4"},{"id":"1.4.2","parentId":"1.4"},{"id":"1.4.3","parentId":"1.4"},{"id":"1.5","parentId":"1"},{"id":"1.5.1","parentId":"1.5"},{"id":"1.5.2","parentId":"1.5"},{"id":"1.5.2.1","parentId":"1.5.2"},{"id":"1.5.2.2","parentId":"1.5.2"},{"id":"1.5.2.3","parentId":"1.5.2"},{"id":"1.5.2.4","parentId":"1.5.2"},{"id":"1.5.2.5","parentId":"1.5.2"},{"id":"1.5.2.6","parentId":"1.5.2"},{"id":"1.5.2.7","parentId":"1.5.2"},{"id":"1.5.2.8","parentId":"1.5.2"},{"id":"1.5.2.9","parentId":"1.5.2"},{"id":"1.5.3","parentId":"1.5"},{"id":"1.5.3.1","parentId":"1.5.3"},{"id":"1.5.3.2","parentId":"1.5.3"},{"id":"1.5.3.2.1","parentId":"1.5.3.2"},{"id":"1.5.3.2.2","parentId":"1.5.3.2"},{"id":"1.5.3.2.3","parentId":"1.5.3.2"},{"id":"1.5.3.3","parentId":"1.5.3"},{"id":"1.5.3.3.1","parentId":"1.5.3.3"},{"id":"1.5.3.3.2","parentId":"1.5.3.3"},{"id":"1.5.3.3.3","parentId":"1.5.3.3"},{"id":"1.5.3.4","parentId":"1.5.3"},{"id":"1.5.4","parentId":"1.5"},{"id":"1.5.4.1","parentId":"1.5.4"},{"id":"1.5.4.2","parentId":"1.5.4"},{"id":"1.5.4.3","parentId":"1.5.4"},{"id":"1.5.4.4","parentId":"1.5.4"},{"id":"1.5.4.5","parentId":"1.5.4"},{"id":"1.5.4.6","parentId":"1.5.4"},{"id":"1.5.4.7","parentId":"1.5.4"},{"id":"1.5.4.8","parentId":"1.5.4"},{"id":"1.6","parentId":"1"},{"id":"1.6.1","parentId":"1.6"},{"id":"1.6.2","parentId":"1.6"},{"id":"1.6.3","parentId":"1.6"},{"id":"1.6.4","parentId":"1.6"},{"id":"1.6.5","parentId":"1.6"},{"id":"1.6.6","parentId":"1.6"},{"id":"1.6.7","parentId":"1.6"},{"id":"1.6.8","parentId":"1.6"},{"id":"1.7","parentId":"1"},{"id":"1.7.1","parentId":"1.7"},{"id":"1.7.10","parentId":"1.7"},{"id":"1.7.11","parentId":"1.7"},{"id":"1.7.12","parentId":"1.7"},{"id":"1.7.13","parentId":"1.7"},{"id":"1.7.14","parentId":"1.7"},{"id":"1.7.15","parentId":"1.7"},{"id":"1.7.16","parentId":"1.7"},{"id":"1.7.2","parentId":"1.7"},{"id":"1.7.3","parentId":"1.7"},{"id":"1.7.4","parentId":"1.7"},{"id":"1.7.5","parentId":"1.7"},{"id":"1.7.6","parentId":"1.7"},{"id":"1.7.7","parentId":"1.7"},{"id":"1.7.8","parentId":"1.7"},{"id":"1.7.9","parentId":"1.7"},{"id":"1.8","parentId":"1"},{"id":"1.8.1","parentId":"1.8"},{"id":"1.8.1.1","parentId":"1.8.1"},{"id":"1.8.1.2","parentId":"1.8.1"},{"id":"1.8.1.3","parentId":"1.8.1"},{"id":"1.8.1.4","parentId":"1.8.1"},{"id":"1.8.1.5","parentId":"1.8.1"},{"id":"1.8.1.6","parentId":"1.8.1"},{"id":"1.8.1.7","parentId":"1.8.1"},{"id":"1.8.1.7.1","parentId":"1.8.1.7"},{"id":"1.8.1.7.2","parentId":"1.8.1.7"},{"id":"1.8.1.7.3","parentId":"1.8.1.7"},{"id":"1.8.2","parentId":"1.8"},{"id":"1.8.2.1","parentId":"1.8.2"},{"id":"1.8.2.2","parentId":"1.8.2"},{"id":"1.8.2.2.1","parentId":"1.8.2.2"},{"id":"1.8.2.2.2","parentId":"1.8.2.2"},{"id":"1.8.2.2.3","parentId":"1.8.2.2"},{"id":"1.8.2.3","parentId":"1.8.2"},{"id":"1.9","parentId":"1"},{"id":"1.9.1","parentId":"1.9"},{"id":"1.9.1.1","parentId":"1.9.1"},{"id":"1.9.1.2","parentId":"1.9.1"},{"id":"1.9.1.3","parentId":"1.9.1"},{"id":"1.9.1.4","parentId":"1.9.1"},{"id":"1.9.1.5","parentId":"1.9.1"},{"id":"1.9.1.6","parentId":"1.9.1"},{"id":"1.9.1.7","parentId":"1.9.1"},{"id":"1.9.1.8","parentId":"1.9.1"},{"id":"1.9.1.9","parentId":"1.9.1"},{"id":"1.9.10","parentId":"1.9"},{"id":"1.9.10.1","parentId":"1.9.10"},{"id":"1.9.10.2","parentId":"1.9.10"},{"id":"1.9.10.3","parentId":"1.9.10"},{"id":"1.9.10.4","parentId":"1.9.10"},{"id":"1.9.10.5","parentId":"1.9.10"},{"id":"1.9.11","parentId":"1.9"},{"id":"1.9.11.1","parentId":"1.9.11"},{"id":"1.9.11.2","parentId":"1.9.11"},{"id":"1.9.11.3","parentId":"1.9.11"},{"id":"1.9.11.4","parentId":"1.9.11"},{"id":"1.9.11.5","parentId":"1.9.11"},{"id":"1.9.11.6","parentId":"1.9.11"},{"id":"1.9.11.7","parentId":"1.9.11"},{"id":"1.9.12","parentId":"1.9"},{"id":"1.9.13","parentId":"1.9"},{"id":"1.9.2","parentId":"1.9"},{"id":"1.9.2.1","parentId":"1.9.2"},{"id":"1.9.2.2","parentId":"1.9.2"},{"id":"1.9.2.3","parentId":"1.9.2"},{"id":"1.9.2.4","parentId":"1.9.2"},{"id":"1.9.2.5","parentId":"1.9.2"},{"id":"1.9.2.6","parentId":"1.9.2"},{"id":"1.9.3","parentId":"1.9"},{"id":"1.9.4","parentId":"1.9"},{"id":"1.9.5","parentId":"1.9"},{"id":"1.9.6","parentId":"1.9"},{"id":"1.9.7","parentId":"1.9"},{"id":"1.9.7.1","parentId":"1.9.7"},{"id":"1.9.7.1.1","parentId":"1.9.7.1"},{"id":"1.9.7.1.2","parentId":"1.9.7.1"},{"id":"1.9.7.1.3","parentId":"1.9.7.1"},{"id":"1.9.7.1.4","parentId":"1.9.7.1"},{"id":"1.9.7.1.5","parentId":"1.9.7.1"},{"id":"1.9.7.2","parentId":"1.9.7"},{"id":"1.9.7.2.1","parentId":"1.9.7.2"},{"id":"1.9.7.2.2","parentId":"1.9.7.2"},{"id":"1.9.7.2.3","parentId":"1.9.7.2"},{"id":"1.9.7.2.4","parentId":"1.9.7.2"},{"id":"1.9.7.2.5","parentId":"1.9.7.2"},{"id":"1.9.7.3","parentId":"1.9.7"},{"id":"1.9.7.3.1","parentId":"1.9.7.3"},{"id":"1.9.7.3.2","parentId":"1.9.7.3"},{"id":"1.9.7.3.3","parentId":"1.9.7.3"},{"id":"1.9.7.3.4","parentId":"1.9.7.3"},{"id":"1.9.7.3.5","parentId":"1.9.7.3"},{"id":"1.9.7.4","parentId":"1.9.7"},{"id":"1.9.8","parentId":"1.9"},{"id":"1.9.8.1","parentId":"1.9.8"},{"id":"1.9.8.1.1","parentId":"1.9.8.1"},{"id":"1.9.8.1.2","parentId":"1.9.8.1"},{"id":"1.9.8.1.3","parentId":"1.9.8.1"},{"id":"1.9.8.1.4","parentId":"1.9.8.1"},{"id":"1.9.8.1.5","parentId":"1.9.8.1"},{"id":"1.9.8.10","parentId":"1.9.8"},{"id":"1.9.8.11","parentId":"1.9.8"},{"id":"1.9.8.11.1","parentId":"1.9.8.11"},{"id":"1.9.8.11.2","parentId":"1.9.8.11"},{"id":"1.9.8.11.3","parentId":"1.9.8.11"},{"id":"1.9.8.12","parentId":"1.9.8"},{"id":"1.9.8.13","parentId":"1.9.8"},{"id":"1.9.8.14","parentId":"1.9.8"},{"id":"1.9.8.15","parentId":"1.9.8"},{"id":"1.9.8.16","parentId":"1.9.8"},{"id":"1.9.8.17","parentId":"1.9.8"},{"id":"1.9.8.18","parentId":"1.9.8"},{"id":"1.9.8.19","parentId":"1.9.8"},{"id":"1.9.8.2","parentId":"1.9.8"},{"id":"1.9.8.20","parentId":"1.9.8"},{"id":"1.9.8.21","parentId":"1.9.8"},{"id":"1.9.8.22","parentId":"1.9.8"},{"id":"1.9.8.23","parentId":"1.9.8"},{"id":"1.9.8.24","parentId":"1.9.8"},{"id":"1.9.8.25","parentId":"1.9.8"},{"id":"1.9.8.25.1","parentId":"1.9.8.25"},{"id":"1.9.8.25.2","parentId":"1.9.8.25"},{"id":"1.9.8.25.3","parentId":"1.9.8.25"},{"id":"1.9.8.26","parentId":"1.9.8"},{"id":"1.9.8.27","parentId":"1.9.8"},{"id":"1.9.8.27.1","parentId":"1.9.8.27"},{"id":"1.9.8.27.2","parentId":"1.9.8.27"},{"id":"1.9.8.3","parentId":"1.9.8"},{"id":"1.9.8.4","parentId":"1.9.8"},{"id":"1.9.8.5","parentId":"1.9.8"},{"id":"1.9.8.5.1","parentId":"1.9.8.5"},{"id":"1.9.8.5.2","parentId":"1.9.8.5"},{"id":"1.9.8.5.3","parentId":"1.9.8.5"},{"id":"1.9.8.6","parentId":"1.9.8"},{"id":"1.9.8.6.1","parentId":"1.9.8.6"},{"id":"1.9.8.6.2","parentId":"1.9.8.6"},{"id":"1.9.8.7","parentId":"1.9.8"},{"id":"1.9.8.8","parentId":"1.9.8"},{"id":"1.9.8.8.1","parentId":"1.9.8.8"},{"id":"1.9.8.8.2","parentId":"1.9.8.8"},{"id":"1.9.8.9","parentId":"1.9.8"},{"id":"1.9.9","parentId":"1.9"},{"id":"1.9.9.1","parentId":"1.9.9"},{"id":"1.9.9.10","parentId":"1.9.9"},{"id":"1.9.9.11","parentId":"1.9.9"},{"id":"1.9.9.12","parentId":"1.9.9"},{"id":"1.9.9.13","parentId":"1.9.9"},{"id":"1.9.9.2","parentId":"1.9.9"},{"id":"1.9.9.3","parentId":"1.9.9"},{"id":"1.9.9.3.1","parentId":"1.9.9.3"},{"id":"1.9.9.3.10","parentId":"1.9.9.3"},{"id":"1.9.9.3.2","parentId":"1.9.9.3"},{"id":"1.9.9.3.3","parentId":"1.9.9.3"},{"id":"1.9.9.3.4","parentId":"1.9.9.3"},{"id":"1.9.9.3.5","parentId":"1.9.9.3"},{"id":"1.9.9.3.6","parentId":"1.9.9.3"},{"id":"1.9.9.3.7","parentId":"1.9.9.3"},{"id":"1.9.9.3.8","parentId":"1.9.9.3"},{"id":"1.9.9.3.9","parentId":"1.9.9.3"},{"id":"1.9.9.4","parentId":"1.9.9"},{"id":"1.9.9.5","parentId":"1.9.9"},{"id":"1.9.9.6","parentId":"1.9.9"},{"id":"1.9.9.7","parentId":"1.9.9"},{"id":"1.9.9.8","parentId":"1.9.9"},{"id":"1.9.9.9","parentId":"1.9.9"}];
let res = [...new Set(temp2.flatMap(function get(id) {
const obj = temp1.find(y => y.id === id);
return [obj, ...obj.parentId !== '1' && get(obj.parentId) || []];
}))];
console.log(res);

How to update a javascript object keys value using the index of an object [duplicate]

If you have an array as part of your state, and that array contains objects, whats an easy way to update the state with a change to one of those objects?
Example, modified from the tutorial on react:
var CommentBox = React.createClass({
getInitialState: function() {
return {data: [
{ id: 1, author: "john", text: "foo" },
{ id: 2, author: "bob", text: "bar" }
]};
},
handleCommentEdit: function(id, text) {
var existingComment = this.state.data.filter({ function(c) { c.id == id; }).first();
var updatedComments = ??; // not sure how to do this
this.setState({data: updatedComments});
}
}
I quite like doing this with Object.assign rather than the immutability helpers.
handleCommentEdit: function(id, text) {
this.setState({
data: this.state.data.map(el => (el.id === id ? Object.assign({}, el, { text }) : el))
});
}
I just think this is much more succinct than splice and doesn't require knowing an index or explicitly handling the not found case.
If you are feeling all ES2018, you can also do this with spread instead of Object.assign
this.setState({
data: this.state.data.map(el => (el.id === id ? {...el, text} : el))
});
While updating state the key part is to treat it as if it is immutable. Any solution would work fine if you can guarantee it.
Here is my solution using immutability-helper:
jsFiddle:
var update = require('immutability-helper');
handleCommentEdit: function(id, text) {
var data = this.state.data;
var commentIndex = data.findIndex(function(c) {
return c.id == id;
});
var updatedComment = update(data[commentIndex], {text: {$set: text}});
var newData = update(data, {
$splice: [[commentIndex, 1, updatedComment]]
});
this.setState({data: newData});
},
Following questions about state arrays may also help:
Correct modification of state arrays in ReactJS
what is the preferred way to mutate a React state?
I'm trying to explain better how to do this AND what's going on.
First, find the index of the element you're replacing in the state array.
Second, update the element at that index
Third, call setState with the new collection
import update from 'immutability-helper';
// this.state = { employees: [{id: 1, name: 'Obama'}, {id: 2, name: 'Trump'}] }
updateEmployee(employee) {
const index = this.state.employees.findIndex((emp) => emp.id === employee.id);
const updatedEmployees = update(this.state.employees, {$splice: [[index, 1, employee]]}); // array.splice(start, deleteCount, item1)
this.setState({employees: updatedEmployees});
}
Edit: there's a much better way to do this w/o a 3rd party library
const index = this.state.employees.findIndex(emp => emp.id === employee.id);
employees = [...this.state.employees]; // important to create a copy, otherwise you'll modify state outside of setState call
employees[index] = employee;
this.setState({employees});
You can do this with multiple way, I am going to show you that I mostly used. When I am working with arrays in react usually I pass a custom attribute with current index value, in the example below I have passed data-index attribute, data- is html 5 convention.
Ex:
//handleChange method.
handleChange(e){
const {name, value} = e,
index = e.target.getAttribute('data-index'), //custom attribute value
updatedObj = Object.assign({}, this.state.arr[i],{[name]: value});
//update state value.
this.setState({
arr: [
...this.state.arr.slice(0, index),
updatedObj,
...this.state.arr.slice(index + 1)
]
})
}

How to rename an object property values removing a specific object from the list

I have a list of object
let table = [{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]
I want to rename the id value after removing an object from the list which has a specific id value(for example id:5)
I am using array filter method
table.filter((element,index)=>{
if(element.id!==5){
element.id=index
return element
}else{
index+1
}
return null
})
I am expecting a return value
[{id: 0,val: "21321"},{id: 1,val: "345345"}]
but i am getting this
[{id: 0, val: "21321"},{id: 2, val: "345345"}]
Note: I know i can use filter method to remove the specific object and than use map method to rename the id value but i want a solution where i have to use only one arrow function
You can use array#reduce to update the indexes and remove elements with given id. For the matched id element, simply return the accumulator and for other, add new object with val and updated id.
const data = [{ id: 4, val: "21321" }, { id: 5, val: "435345" }, { id: 6, val: "345345" }],
result = data.reduce((res, {id, val}) => {
if(id === 5) {
return res;
}
res.push({id: res.length + 1, val});
return res;
}, []);
console.log(result)
This would do it:
let table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]];
let res=table[0].reduce((a,c)=>{if(c.id!=5){
c.id=a.length;
a.push(c)
}
return a}, []);
console.log([res])
The only way to do it with "a single arrow function" is using .reduce().
Here is an extremely shortened (one-liner) version of the same:
let res=table[0].reduce((a,c)=>(c.id!=5 && a.push(c.id=a.length,c),a),[]);
Actually, I was a bit premature with my remark about the "only possible solution". Here is a modified version of your approach using .filter() in combination with an IIFE ("immediately invoked functional expression"):
table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]];
res= [ (i=>table[0].filter((element,index)=>{
if(element.id!==5){
element.id=i++
return element
} }))(0) ];
console.log(res)
This IIFE is a simple way of introducing a persistent local variable i without polluting the global name space. But, stricly speaking, by doing that I have introduced a second "arrow function" ...
It probably is not the best practice to use filter and also alter the objects at the same time. But you would need to keep track of the count as you filter.
let table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]]
const removeReorder = (data, id) => {
var count = 0;
return data.filter(obj => {
if (obj.id !== id) {
obj.id = count++;
return true;
}
return false;
});
}
console.log(removeReorder(table[0], 5));
It is possible to achieve desired result by using reduce method:
const result = table.reduce((a, c) => {
let nestedArray = [];
c.forEach(el => {
if (el.id != id)
nestedArray.push({ id: nestedArray.length, val: el.val });
});
a.push(nestedArray);
return a;
}, [])
An example:
let table = [[{ id: 4, val: "21321" }, { id: 5, val: "435345" }, { id: 6, val: "345345" }]]
let id = 5;
const result = table.reduce((a, c) => {
let nestedArray = [];
c.forEach(el => {
if (el.id != id)
nestedArray.push({ id: nestedArray.length, val: el.val });
});
a.push(nestedArray);
return a;
}, [])
console.log(result);
The main issue in your code is filter method is not returning boolean value. Use the filter method to filter items and then use map to alter object.
let table = [
[
{ id: 4, val: "21321" },
{ id: 5, val: "435345" },
{ id: 6, val: "345345" },
],
];
const res = table[0]
.filter(({ id }) => id !== 5)
.map(({ val }, i) => ({ id: i, val }));
console.log(res)
Alternatively, using forEach with one iteration
let table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]]
const res = [];
let i = 0;
table[0].forEach(({id, val}) => id !== 5 && res.push({id: i++, val}));
console.log(res)

Get unique keys only from javascript array, where keys and value both are dynamic

This is my object-
[{"index":"style_bags","name":"24"},
{"index":"style_bags","name":"25"},
{"index":"style_bags","name":"26"},
{"index":"category_gear","name":"90"},
{"index":"category_gear","name":"98"},
{"index":"price","name":"400-"}]
This is expected -
[{"index":"style_bags","name":"26"},
{"index":"category_gear","name":"98"},
{"index":"price","name":"400-"}]
Requirement is to get last value and key pair from object in javascript.
As per asked from Certain Performance The way I reached here was through this code -
if(href.indexOf("&") >= 0){
var hrefSplit = href.split('&');
}
if(!(href.indexOf("&") >= 0)){
var hrefSplit = href.split('?');
var hrefSplit2 = hrefSplit[1].split('=');
}
params.push({ index: hrefSplit2[0], name: hrefSplit2[1]});
var i = 0;
jquery.each(params, function( index, value ) {
var key = value.index;
if(hrefSplit2[0] == key){
i++;
}
});
valueGet.push({ index: hrefSplit2[0], name: hrefSplit2[1]});
I am not a javascript pro, so was kind of blank on how to proceed further.
I will make a temp object storing the last found values and then create the resulting array.
let tempObj = {}
let srcArray = [
{"index":"style_bags","name":"24"},
{"index":"style_bags","name":"25"},
{"index":"style_bags","name":"26"},
{"index":"category_gear","name":"90"},
{"index":"category_gear","name":"98"},
{"index":"price","name":"400-"}];
for (let item of srcArray) {
tempObj[item.index] = item.name
}
let result = Object.keys(tempObj).map(index => {
return {
index,
name: tempObj[index]
}
});
console.log(result)
You can use reduce to create an object and then Object.values to get array of values. It will get last object based on index property.
const data = [{"index":"style_bags","name":"24"}, {"index":"style_bags","name":"25"}, {"index":"style_bags","name":"26"}, {"index":"category_gear","name":"90"}, {"index":"category_gear","name":"98"}, {"index":"price","name":"400-"}]
const result = data.reduce((r, e) => Object.assign(r, {[e.index]: e}), {})
console.log(Object.values(result))
You could use a Map and collect the last object with the same index property.
var array = [{ index: "style_bags", name: "24" }, { index: "style_bags", name: "25" }, { index: "style_bags", name: "26" }, { index: "category_gear", name: "90" }, { index: "category_gear", name: "98" }, { index: "price", name: "400-" }],
result = [...new Map(array.map(o => [o.index, o])).values()];
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You inspired me to update my javascript linq library to support .last(). So, using this library, you could:
var list = [{"index":"style_bags","name":"24"},
{"index":"style_bags","name":"25"},
{"index":"style_bags","name":"26"},
{"index":"category_gear","name":"90"},
{"index":"category_gear","name":"98"},
{"index":"price","name":"400-"}];
var filtered = loq(list).groupBy(x => x.index).select(g => g.last());
console.log(filtered.toArray());
<script src="https://cdn.rawgit.com/biggyspender/loq/cb4e5cb4/lib/loq.min.js"></script>

find and modify deeply nested object in javascript array

I have an array of objects that can be of any length and any depth. I need to be able to find an object by its id and then modify that object within the array. Is there an efficient way to do this with either lodash or pure js?
I thought I could create an array of indexes that led to the object but constructing the expression to access the object with these indexes seems overly complex / unnecessary
edit1; thanks for all yours replies I will try and be more specific. i am currently finding the location of the object I am trying to modify like so. parents is an array of ids for each parent the target object has. ancestors might be a better name for this array. costCenters is the array of objects that contains the object I want to modify. this function recurses and returns an array of indexes that lead to the object I want to modify
var findAncestorsIdxs = function(parents, costCenters, startingIdx, parentsIdxs) {
var idx = startingIdx ? startingIdx : 0;
var pidx = parentsIdxs ? parentsIdxs : [];
_.each(costCenters, function(cc, ccIdx) {
if(cc.id === parents[idx]) {
console.log(pidx);
idx = idx + 1;
pidx.push(ccIdx);
console.log(pidx);
pidx = findAncestorsIdx(parents, costCenters[ccIdx].children, idx, pidx);
}
});
return pidx;
};
Now with this array of indexes how do I target and modify the exact object I want? I have tried this where ancestors is the array of indexes, costCenters is the array with the object to be modified and parent is the new value to be assigned to the target object
var setParentThroughAncestors = function(ancestors, costCenters, parent) {
var ccs = costCenters;
var depth = ancestors.length;
var ancestor = costCenters[ancestors[0]];
for(i = 1; i < depth; i++) {
ancestor = ancestor.children[ancestors[i]];
}
ancestor = parent;
console.log(ccs);
return ccs;
};
this is obviously just returning the unmodified costCenters array so the only other way I can see to target that object is to construct the expression like myObjects[idx1].children[2].grandchildren[3].ggranchildren[4].something = newValue. is that the only way? if so what is the best way to do that?
You can use JSON.stringify for this. It provides a callback for each visited key/value pair (at any depth), with the ability to skip or replace.
The function below returns a function which searches for objects with the specified ID and invokes the specified transform callback on them:
function scan(id, transform) {
return function(obj) {
return JSON.parse(JSON.stringify(obj, function(key, value) {
if (typeof value === 'object' && value !== null && value.id === id) {
return transform(value);
} else {
return value;
}
}));
}
If as the problem is stated, you have an array of objects, and a parallel array of ids in each object whose containing objects are to be modified, and an array of transformation functions, then it's just a matter of wrapping the above as
for (i = 0; i < objects.length; i++) {
scan(ids[i], transforms[i])(objects[i]);
}
Due to restrictions on JSON.stringify, this approach will fail if there are circular references in the object, and omit functions, regexps, and symbol-keyed properties if you care.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter for more info.
As Felix Kling said, you can iterate recursively over all objects.
// Overly-complex array
var myArray = {
keyOne: {},
keyTwo: {
myId: {a: '3'}
}
};
var searchId = 'myId', // Your search key
foundValue, // Populated with the searched object
found = false; // Internal flag for iterate()
// Recursive function searching through array
function iterate(haystack) {
if (typeof haystack !== 'object' || haystack === null) return; // type-safety
if (typeof haystack[searchId] !== 'undefined') {
found = true;
foundValue = haystack[searchId];
return;
} else {
for (var i in haystack) {
// avoid circular reference infinite loop & skip inherited properties
if (haystack===haystack[i] || !haystack.hasOwnProperty(i)) continue;
iterate(haystack[i]);
if (found === true) return;
}
}
}
// USAGE / RESULT
iterate(myArray);
console.log(foundValue); // {a: '3'}
foundValue.b = 4; // Updating foundValue also updates myArray
console.log(myArray.keyTwo.myId); // {a: '3', b: 4}
All JS object assignations are passed as reference in JS. See this for a complete tutorial on objects :)
Edit: Thanks #torazaburo for suggestions for a better code.
If each object has property with the same name that stores other nested objects, you can use: https://github.com/dominik791/obj-traverse
findAndModifyFirst() method should solve your problem. The first parameter is a root object, not array, so you should create it at first:
var rootObj = {
name: 'rootObject',
children: [
{
'name': 'child1',
children: [ ... ]
},
{
'name': 'child2',
children: [ ... ]
}
]
};
Then use findAndModifyFirst() method:
findAndModifyFirst(rootObj, 'children', { id: 1 }, replacementObject)
replacementObject is whatever object that should replace the object that has id equal to 1.
You can try it using demo app:
https://dominik791.github.io/obj-traverse-demo/
Here's an example that extensively uses lodash. It enables you to transform a deeply nested value based on its key or its value.
const _ = require("lodash")
const flattenKeys = (obj, path = []) => (!_.isObject(obj) ? { [path.join('.')]: obj } : _.reduce(obj, (cum, next, key) => _.merge(cum, flattenKeys(next, [...path, key])), {}));
const registrations = [{
key: "123",
responses:
{
category: 'first',
},
}]
function jsonTransform (json, conditionFn, modifyFn) {
// transform { responses: { category: 'first' } } to { 'responses.category': 'first' }
const flattenedKeys = Object.keys(flattenKeys(json));
// Easily iterate over the flat json
for(let i = 0; i < flattenedKeys.length; i++) {
const key = flattenedKeys[i];
const value = _.get(json, key)
// Did the condition match the one we passed?
if(conditionFn(key, value)) {
// Replace the value to the new one
_.set(json, key, modifyFn(key, value))
}
}
return json
}
// Let's transform all 'first' values to 'FIRST'
const modifiedCategory = jsonTransform(registrations, (key, value) => value === "first", (key, value) => value = value.toUpperCase())
console.log('modifiedCategory --', modifiedCategory)
// Outputs: modifiedCategory -- [ { key: '123', responses: { category: 'FIRST' } } ]
I needed to modify deeply nested objects too, and found no acceptable tool for that purpose. Then I've made this and pushed it to npm.
https://www.npmjs.com/package/find-and
This small [TypeScript-friendly] lib can help with modifying nested objects in a lodash manner. E.g.,
var findAnd = require("find-and");
const data = {
name: 'One',
description: 'Description',
children: [
{
id: 1,
name: 'Two',
},
{
id: 2,
name: 'Three',
},
],
};
findAnd.changeProps(data, { id: 2 }, { name: 'Foo' });
outputs
{
name: 'One',
description: 'Description',
children: [
{
id: 1,
name: 'Two',
},
{
id: 2,
name: 'Foo',
},
],
}
https://runkit.com/embed/bn2hpyfex60e
Hope this could help someone else.
I wrote this code recently to do exactly this, as my backend is rails and wants keys like:
first_name
and my front end is react, so keys are like:
firstName
And these keys are almost always deeply nested:
user: {
firstName: "Bob",
lastName: "Smith",
email: "bob#email.com"
}
Becomes:
user: {
first_name: "Bob",
last_name: "Smith",
email: "bob#email.com"
}
Here is the code
function snakeCase(camelCase) {
return camelCase.replace(/([A-Z])/g, "_$1").toLowerCase()
}
export function snakeCasedObj(obj) {
return Object.keys(obj).reduce(
(acc, key) => ({
...acc,
[snakeCase(key)]: typeof obj[key] === "object" ? snakeCasedObj(obj[key]) : obj[key],
}), {},
);
}
Feel free to change the transform to whatever makes sense for you!

Categories