'how to remove a property from an array of objects
How to remove a property from an array of objects
need to be removed first _id, usedDays, put two arrays (start and end) into the one and deploy "user" in userId and name.
Have array:
[{
"_id": "someID",
"user": {
"_id": "mongoID",
"name": "Vasya"
},
"usedDays": 1,
"start": "2021-07-02",
"end": "2021-07-21"
},
{
"_id": "someID",
"user": {
"_id": "mongoID",
"name": "Ivan"
},
"usedDays": 5,
"start": "2021-08-12",
"end": "2021-08-22"
}
]
Need array:
[{
"userId": "mongoID",
"name": "Vasya"
"partyDate": ["start": "2021-07-02","end": "2021-07-21"]
},
{
"userId": "mongoID",
"name": "Ivan"
"partyDate": ["start": "2021-08-12","end": "2021-08-22"]
}
]
Solution 1:[1]
I think Array.map is what you are looking for
array.map((item) => {
return ({
userId: item.user._id,
name: item.user.name,
partyDate: [{start: item.start, end: item.end}]
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jnbjarni |
