var result = [
{
color: "blue",
users: [
{
"name": "John",
"color": "blue",
"age": "29"
},
{
"name": "Neil",
"color": "blue",
"age": "34"
}
]
},
{
color: "green",
users: [
{
"name": "Ronn",
"color": "green",
"age": "50"
}
]
}
]
I want to delete the color
key under users
. For this, I have written the following code snippet in Lodash.
var result = _.omit(result.users, ['color']);
But it gives me the following { ... }
How can achieve the following output?
[
{
color: "blue",
users: [
{
"name": "John",
"age": "29"
},
{
"name": "Neil",
"age": "34"
}
]
},
{
color: "green",
users: [
{
"name": "Ronn",
"age": "50"
}
]
}
]
CodePudding user response:
You have to loop over the array and use
You can easily achieve the result using vanilla JS using map
as:
var result = [
{
color: "blue",
users: [
{
name: "John",
color: "blue",
age: "29",
},
{
name: "Neil",
color: "blue",
age: "34",
},
],
},
{
color: "green",
users: [
{
name: "Ronn",
color: "green",
age: "50",
},
],
},
];
const res = result.map((obj) => ({ ...obj, users: obj.users.map(({ color, ...rest }) => rest)}));
console.log(res);