How to fetch data from complex array objects structures? - javascript

I have such an object
data: {
dataFirst: {
Food: [ {id: 536131, name: "option1", }]
},
dataSecond: {
Autos: [{id: 678, name: 'option1'}],
Houses: [
{id: 6876, name: "option1"},
{id: 6876, name: "Placed App"},
],
Phones: [
{id: 672, name: "option1"},
{id: 97249, name: "Placed},
],
Food: [
{id: 772, name: "option1"},
{id: 6777, name: "Placed},
],
}
}
The problem is, that I may have same data in dataFirst and dataSecond, for examle 'Food', I have 2 array objects that contains different data but I need to make it one object 'Food' with the data from 2 of them, from the dataFirst 'Food' and dataSecond 'Food'. I had such a code:
export const parser = ({ data }) => {
const result = Object.values(data).reduce((prev, topicsGroup) => {
Object.assign(prev, topicsGroup);
return prev;
}, {});
return result;
}
but this code doesn't unite 2 'Food' objects but returns data only from the dataFirst 'Food' object wihout second one.

You can iterate through all values of your main data object with Object.values(data) and combine them with reduce by concatenating arrays corresponding to common keys:
let data = {
dataFirst: {
Food: [{
id: 536131,
name: "option1",
}]
},
dataSecond: {
Autos: [{
topicId: 678,
name: 'option1'
}],
Houses: [{
topicId: 6876,
name: "option1"
},
{
topicId: 6876,
topicName: "Placed App"
},
],
Phones: [{
topicId: 672,
name: "option1"
},
{
topicId: 97249,
name: "Placed"
},
],
Food: [{
topicId: 772,
name: "option1"
},
{
topicId: 6777,
name: "Placed"
},
],
}
};
let res = Object.values(data).reduce((acc, curr) => {
Object.entries(curr).forEach(([k, v]) => {
if (k in acc) acc[k] = acc[k].concat(v);
else acc[k] = v;
});
return acc;
}, {});
console.log(res);

Related

RXJS, add new key:value to nested observable

I'm new with rxjs and i can't see how can i do what i need. I need to add a pair active:true recursively.
I have this data structure coming from api as observable:
var data = [
{
name: "hom",
subcategories: [
{ name: "reb" },
{ name: "nov" },
{ name: "otr" }
]
},
{
name: "muj",
subcategories: [
{ name: "REB" },
{ name: "NOV" },
{ name: "OT" }
]
},
{
name: "ni",
subcategories: [
{ name: "REB", subcategories: [
{name: "rebanis"},
{name: "rebawos"}] },
{ name: "NOV", subcategories: [
{name: "novebanis"},
{name: "novrebawos"}] },
{ name: "OT", subcategories: [
{name: "otrebanis"},
{name: "otrebawos"}]
}
]
}
];
And what i want is to add a active:true on every level that have subcategories, and i don't know how many nested subcategories could be.
This is the result that i want:
var data = [
{
active: true,
name: "hom",
subcategories: [
{ name: "reb" },
{ name: "nov" },
{ name: "otr" }
]
},
{
active: true,
name: "muj",
subcategories: [
{ name: "REB" },
{ name: "NOV" },
{ name: "OT" }
]
},
{
name: "ni",
active: true,
subcategories: [
{ name: "REB", active: true, subcategories: [
{name: "rebanis"},
{name: "rebawos"}] },
{ name: "NOV", active: true, subcategories: [
{name: "novebanis"},
{name: "novrebawos"}] },
{ name: "OT", active: true, subcategories: [
{name: "otrebanis"},
{name: "otrebawos"}]
}
]
}
];
I tried something like this:
const req = of(data)
.pipe(
map(data => {
const transformedData = Object.keys(data).map(key =>
Object.assign(data[key], { active: true })
);
return transformedData;
})
)
.subscribe(output => console.log(output));
But the result is not clear and it only affects on first level not on nested.
You can use map inside recursive function:
const data = [ { name: "hom", subcategories: [ { name: "reb" }, { name: "nov" }, { name: "otr" } ] }, { name: "muj", subcategories: [ { name: "REB" }, { name: "NOV" }, { name: "OT" } ] }, { name: "ni", subcategories: [ { name: "REB", subcategories: [ {name: "rebanis"}, {name: "rebawos"}] }, { name: "NOV", subcategories: [ {name: "novebanis"}, {name: "novrebawos"}] }, { name: "OT", subcategories: [ {name: "otrebanis"}, {name: "otrebawos"}] } ] } ];
const addNewProp=data=>data.map(o=>o.subcategories ? ({...o, active:true, subcategories: addNewProp(o.subcategories)}) : o);
console.log(addNewProp(data));
The question has nothing to do with rxJs and Observable, it is a problem of transformation of an Array.
I would try something like this
data.map(el => {
if(el.subcategories && el.subcategories.lenght > 0) {
const newEl = {...el};
newEl.active = true;
return newEl;
}
return {...el}
})
Notice that here I am using some sort of immutability and hence I create new elements within the loop. If this is not required in your case, you can simplify the code like this
data.map(el => {
if(el.subcategories && el.subcategories.lenght > 0) {
el.active = true;
}
return el
})

How to filter array based on nested value in Javascript

I have an array which I'm trying to filter based on a certain nested value, without getting any other elements.
const arrayData = [
{
country: "Country X",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'John', status:'Active' },{ name: 'Rob', status:'Suspended' }]
},
{
gameTitle: 'Game2',
players: [{ name: 'Saly', status:'Blocked' }]
},
]
},
{
country: "Country Y",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'Sindy', status:'Pending' },{ name: 'someone', status:'Rejected' }]
},
{
gameTitle: 'Game2',
players: [{ name: 'Alex', status:'New' },{ name: 'Nic', status:'Old' }]
},
]
},
];
What I have tried:
let output = arrayData.filter(eachVal => {
let opt = eachVal.games.some((
{ players }) => players
.some(({ status}) => status === 'Active'));
return opt;
})
Expected result for finding all players with value status : 'Active':
{
country: "Country X",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'John', status:'Active' }]
}
]
}
But the result:
[{"country":"Country X","games":[{"gameTitle":"Game1","players":[{"name":"John","status":"Active"},{"name":"Rob","status":"Suspended"}]},{"gameTitle":"Game2","players":[{"name":"Saly","status":"Blocked"}]}]}]
Try this:
arrayData.map(({country, games: g}) => {
const games = g.map((g) => {
const players = g.players.filter((p) => p.status === 'Active')
return {...g, players}
})
.filter(({players}) => players.length > 0)
return {country, games}
}).filter(({games}) => games.length>0)
The logic is nested filter through each level and assigning the finds back to their respective places and using forEach logic to see if an object has an "Active" value
const arrayData = [
{
country: "Country X",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'John', status:'Active' },{ name: 'Rob', status:'Suspended' }]
},
{
gameTitle: 'Game2',
players: [{ name: 'Saly', status:'Blocked' }]
},
]
},
{
country: "Country Y",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'Sindy', status:'Pending' },{ name: 'someone', status:'Rejected' }]
},
{
gameTitle: 'Game2',
players: [{ name: 'Alex', status:'New' },{ name: 'Nic', status:'Old' }]
},
]
},
];
//answer
let arr=JSON.parse(JSON.stringify(arrayData))
arr=arr.filter(a=>{
let f=a.games; let i=false
f=f.filter(b=>{
let x=b.players; let j=false
x=x.filter(c=>c.status=="Active")
x.forEach(c=>{if(c.status=="Active"){j=true}})
b.players=x; return j
})
f.forEach(b=>{
b.players.forEach(c=>{if(c.status=="Active"){i=true}})
})
a.games=f; return i
})
console.log(arr)
It's quite easy. You just need some map and filter:
const arrayData = [
{
country: "Country X",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'John', status:'Active' },{ name: 'Rob', status:'Suspended' }]
},
{
gameTitle: 'Game2',
players: [{ name: 'Saly', status:'Blocked' }]
},
]
},
{
country: "Country Y",
games: [
{
gameTitle: 'Game1',
players: [{ name: 'Sindy', status:'Pending' },{ name: 'someone', status:'Rejected' }]
},
{
gameTitle: 'Game2',
players: [{ name: 'Alex', status:'New' },{ name: 'Nic', status:'Old' }]
},
]
},
];
/*------------------------------- This is the answer -------------------------------*/
let result = arrayData.map(a => ({...a, games: a.games.map(g => ({...g, players: g.players.filter(p => p.status === 'Active')})).filter(g => g.players.length)})).filter(a => a.games.length)
/*----------------------------------------------------------------------------------*/
console.log(result)
I'd "recursively" select the valid elements at each level and map on each filtered array to build the correct output:
const selectActivePlayers = (players) => players.filter(player => player.status === "Active");
const selectValidGames = (games) => games.filter(game => selectActivePlayers(game.players).length > 0)
const selectValidCountries = (countries) => countries.filter(country => selectValidGames(country.games).length > 0);
const cleanCountries = selectValidCountries(arrayData).map(country => ({
...country,
games: selectValidGames(country.games).map(game => ({
...game,
players: selectActivePlayers(game.players)
}))
}))
Output:
[
{
"country": "Country X",
"games": [
{
"gameTitle": "Game1",
"players": [
{
"name": "John",
"status": "Active"
}
]
}
]
}
]

How to get unique array from nested object with lodash

My collection contains the following (array of objects):
[
{
id: 'id-1',
uniqueName: 'operation-level-1',
operations: [
{
name: 'operaion-1',
label: 'operation-1-label'
},
{
name: 'operaion-2',
label: 'operation-2-label'
}
]
},
{
id: 'id-2',
uniqueName: 'operation-level-2'
operations: [
{
name: 'operaion-1',
label: 'operation-1-label'
},
{
name: 'operaion-3',
label: 'operation-3-label'
}
]
}
]
I wanted to get an array of unique operation name and label as shown below
const result = [
{
name: 'operaion-1',
label: 'operation-1-label'
},
{
name: 'operaion-2',
label: 'operation-2-label'
},
{
name: 'operaion-3',
label: 'operation-3-label'
}
]
Can someone suggest the best way to achieve this, please?
This can be easily done without lodash. You can first flat the data then map it as a key value pair and then make use of Map to remove the duplicate entries:
var data=[{ id: 'id-1', uniqueName: 'operation-level-1', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-2', label: 'operation-2-label' } ] }, { id: 'id-2', uniqueName: 'operation-level-2', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-3', label: 'operation-3-label' } ] }];
var result = [...new Map(data.flatMap(({operations})=>operations).map(k=>([k.name, k]))).values()];
console.log(result);
Or if you do not want to use Map then use filter method:
var data=[{ id: 'id-1', uniqueName: 'operation-level-1', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-2', label: 'operation-2-label' } ] }, { id: 'id-2', uniqueName: 'operation-level-2', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-3', label: 'operation-3-label' } ] }];
var result = data.flatMap(({operations})=>operations).filter((val,i,self)=>self.findIndex(j=>j.name==val.name && j.label==val.label)==i);
console.log(result);
Use _.flatMap() to get a flattened array of operations, and then use _.uniqBy() to get only items with unique name:
const data=[{ id: 'id-1', uniqueName: 'operation-level-1', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-2', label: 'operation-2-label' } ] }, { id: 'id-2', uniqueName: 'operation-level-2', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-3', label: 'operation-3-label' } ] }];
const result = _.uniqBy(
_.flatMap(data, 'operations'),
'name'
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
With lodash/fp you can generate a function using _.flow() that flattens operations, and then get the unique values by name:
const fn = _.flow(
_.flatMap('operations'),
_.uniqBy('name')
)
const data=[{ id: 'id-1', uniqueName: 'operation-level-1', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-2', label: 'operation-2-label' } ] }, { id: 'id-2', uniqueName: 'operation-level-2', operations: [ { name: 'operaion-1', label: 'operation-1-label' }, { name: 'operaion-3', label: 'operation-3-label' } ] }];
const result = fn(data);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash#4(lodash.min.js+lodash.fp.min.js)'></script>
There is no need for loadash, a simple Map object, will allow you to suppress the duplicates.
const arr = [{
id: 'id-1',
uniqueName: 'operation-level-1',
operations: [{
name: 'operaion-1',
label: 'operation-1-label'
},
{
name: 'operaion-2',
label: 'operation-2-label'
}
]
},
{
id: 'id-2',
uniqueName: 'operation-level-2',
operations: [{
name: 'operaion-1',
label: 'operation-1-label'
},
{
name: 'operaion-3',
label: 'operation-3-label'
}
]
}
];
// Go thought the arr and add all the name/label into the map
// Then use Array.from to reformate the Map into the wanted format
const uniqueArr = Array.from(arr.reduce((tmp, {
operations,
}) => {
operations.forEach(({
name,
label,
}) => {
tmp.set(name, label);
});
return tmp;
}, new Map())).map(([name, label]) => ({
name,
label,
}));
console.log(uniqueArr);

How to flat tree, auto generation parent?

I want to keep the parent-child relationship of the tree node.
I have a JSON tree, like this
{
id: null,
children: [
{
id: 1,
children: [
{
id: 11,
children: [
{
id: 111
children: []
}
]
},
{
id: '12',
children: []
},
]
},
{
id: '2',
children: [
{
id: '21',
children: []
},
{
id: '22',
children: [
{
id: '221',
children: []
}
]
},
]
},
]
}
I want flat the tree, like this
[
{ id: 1, parent: null,},
{ id: 11, parent: 1, },
{ id: 111, parent: 11, },
{ id: 2, parent: null, },
{ id: 21, parent: 2, },
...
]
parent automatic generated
Is there any good way?
You could use flatMap method and create recursive function that will return 1D array as a result.
const data = {"id":null,"children":[{"id":1,"children":[{"id":11,"children":[{"id":111,"children":[]}]},{"id":"12","children":[]}]},{"id":"2","children":[{"id":"21","children":[]},{"id":"22","children":[{"id":"221","children":[]}]}]}]}
const flatten = (data, parent = null) =>
data.flatMap(({ id, children }) => ([
{ id, parent },
...flatten(children, id)
]))
const result = flatten(data.children);
console.log(result)
You could get the object and return an array of the flat children.
const
getFlat = ({ id, children = [] }) =>
children.flatMap(o => [{ id: o.id, parent: id }, ...getFlat(o)]);
var data = { id: null, children: [{ id: 1, children: [{ id: 11, children: [{ id: 111, children: [] }] }, { id: '12', children: [] }] }, { id: '2', children: [{ id: '21', children: [] }, { id: '22', children: [{ id: '221', children: [] }] }] }] },
flat = getFlat(data);
console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If compatbility with Internet Explorer is important, then the following approach which avoids the need for Array#flatMap might suit:
const input={id:null,children:[{id:'1',children:[{id:'11',children:[{id:'111',children:[]}]},{id:'12',children:[]}]},{id:'2',children:[{id:'21',children:[]},{id:'22',children:[{id:'221',children:[]}]}]}]};
const result = [];
// Define recursive function to walk through the data tree
// and produce a flat array of required data
const recurse = (item, parent) => {
// Only add item to result if it has valid id
if (item.id) {
result.push({
id: item.id,
parent: parent ? parent.id : null
});
}
// Iterate the children of this item, traversing and
// processing them in the same way
item.children.forEach(child => recurse(child, item))
}
recurse(input);
console.log(result);
You can use a simple recursion to achieve this
let data = {
id: 1,
name: 'a1',
children: [{
id: 2,
name: 'a2',
children: [{
id: 3,
name: 'b1',
children: []
},
{
id: 4,
name: 'b2',
children: []
}
]
}]
}
function flatten(data){
output = [];
return (function indentHandler(data, level, parent){
output.push({id: data['id'], parent: parent})
if (data['children'].length === 0){
return output;
}
level += 1;
data['children'].forEach(function(child){
return indentHandler(child, level, data.name);
});
return output;
})(data, 0, null);
}
flatten(data);
use recursion
var tree = {
id: null,
children: [{
id: 1,
children: [{
id: 11,
children: [{
id: 111,
children: []
}]
},
{
id: '12',
children: []
},
]
},
{
id: '2',
children: [{
id: '21',
children: []
},
{
id: '22',
children: [{
id: '221',
children: []
}]
},
]
}
]
};
var flat = [];
function flatArray(arr, parentId) {
arr.forEach(function(el) {
flat.push({
id: el.id,
parent: parentId || null
});
if (el.children)
flatArray(el.children, el.id);
});
}
flatArray(tree.children, 0)
console.log(flat);

Add property to each object of specific structure JS? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have an object :
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
How can I add to each object in groups id with the same value as in name prop to get this:
const obj2 = {
app: {
groups: [{ name: 'name', values: 'values', id: 'name' }, { name: 'name1', values: 'values1', id: 'name1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values', id: 'name2' }, { name: 'name7', values: 'values4', id: 'name7' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5', id: 'name5' }, { name: 'name4', values: 'values4', id: 'name4' }]
}
};
Mutable version: If you can modify your input object, use Array.forEach() twice:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
Object.keys(obj).forEach(key => {
obj[key].groups.forEach(x => x.id = x.name);
});
console.log(obj);
Immutable version: If you don't want to modify your input object, use Array.reduce() and the spread operator to copy properties from the inner objects:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
const result = Object.entries(obj).reduce((acc, [key, val]) => {
acc[key] = { groups: val.groups.map(x => ({ ...x, id: x.name })) };
return acc;
}, {});
console.log(result);
In both cases, you'll need to iterate over the keys using Object.keys() or directly over the entries with Object.entries().
Loop through your object and then loop through the array in your inner object:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
for (app in obj) {
obj[app].groups.forEach(innerObj => {
innerObj.id = innerObj.name;
});
}
console.log(obj);
Iterate over Object.keys and use map like so:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
function addIdProp(object) {
let result = object;
Object.keys(result).forEach(key => {
result[key].groups = result[key].groups.map(({ name, values }) => { return { name, values, id: name}});
});
return result;
}
const obj2 = addIdProp(obj);
console.log(obj2);
.as-console-wrapper { max-height: 100% !important; top: 0 }
ES5 syntax:
var obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
function addIdProp(object) {
var result = object;
Object.keys(result).forEach(function(key) {
result[key].groups = result[key].groups.map(function({ name, values }) { return { name: name, values: values, id: name}});
});
return result;
}
var obj2 = addIdProp(obj);
console.log(obj2);
.as-console-wrapper { max-height: 100% !important; top: 0 }
What about with this?
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
obj[k].groups = obj[k].groups.map((g) => {
return { ...g, id: g.name}
})
}
}
console.log(obj);
Loop through the object properties and add their group array a id prop assigning the relevant name with two nested forEaches as follows:
const obj = {
app: {
groups: [{
name: 'name',
values: 'values'
}, {
name: 'name1',
values: 'values1'
}]
},
app2: {
groups: [{
name: 'name2',
values: 'values'
}, {
name: 'name2',
values: 'values4'
}]
},
app3: {
groups: [{
name: 'name5',
values: 'values5'
}, {
name: 'name4',
values: 'values4'
}]
}
};
Object.keys(obj).forEach(e => obj[e].groups.forEach(el => el.id = el.name));
console.log(JSON.stringify(obj))
Note that it does this implementation does not create a new object, instead modifies the initial one. Your use case may welcome or discourage it.
You can try with for..in and forEach
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
for (let item in obj) {
obj[item].groups.forEach(o => o.id = o.name)
}
console.log(obj)
One solution that is purely function, immutable, and independent of scope/closure would be to reduce the [Object.entries()][1] of the obj into a new obj2 result like so:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
/* Reduce entries into a new object */
const obj2 = Object.entries(obj).reduce((newObject, [key, value]) => {
/* For each value of new object array of, map group items to include id : name property */
newObject[ key ] = { groups : value.groups.map(item => ({ ...item, id : item.name })) }
return newObject
}, {})
console.log(obj2)
You have to loop through the object first and then map inside the groups array like that:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1'}]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
var newObj = {...obj};
for(var key in newObj){
newObj[key] = newObj[key].groups.map(item => ({...item, id: item.name}));
}
console.log(newObj); // here is your new object with id's
You can do:
const obj = {app: {groups: [{name: 'name',values: 'values'}, {name: 'name1',values: 'values1'}]},app2: {groups: [{name: 'name2',values: 'values'}, {name: 'name2',values: 'values4'}]},app3: {groups: [{name: 'name5',values: 'values5'}, {name: 'name4',values: 'values4'}]}};
const obj2 = Object.keys(obj).map(k => ({
[k]: {
groups: obj[k].groups.map(o => {
o.id = o.name;
return o;
})
}
}));
console.log(obj2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Categories

Resources