EN
JavaScript - How to delete property from spread operator?
1 answers
0 points
I want to delete group
property from the array of objects but I don't really know how.
Is there any way to delete property from the spread operator?
The object I get from the response looks like this:
xxxxxxxxxx
1
const objects = [
2
{
3
id: 1,
4
name: 'item 1',
5
group: 'A',
6
isValid: false
7
},
8
{
9
id: 2,
10
name: 'item 2',
11
group: 'B',
12
isValid: true
13
}
14
];
Now, what I want to do is to keep everything but group property in the objects:
xxxxxxxxxx
1
const objects = [
2
{
3
id: 1,
4
name: 'item 1',
5
isValid: false
6
},
7
{
8
id: 2,
9
name: 'item 2',
10
isValid: true
11
}
12
];
1 answer
0 points
You can use rest parameters in object destructuring to get all the properties except group
in your array of objects:
xxxxxxxxxx
1
const objects = [
2
{
3
id: 1,
4
name: 'item 1',
5
group: 'A',
6
isValid: false
7
},
8
{
9
id: 2,
10
name: 'item 2',
11
group: 'B',
12
isValid: true
13
}
14
];
15
16
const result = objects.map(({ group, rest }) => rest);
17
18
console.log(JSON.stringify(result, null, 4));
See also
References
0 commentsShow commentsAdd comment