'How to copy an array to another array with one property which is an object
I have an array like this one:
"data": [
{
"_id": {
"uuid": "1",
"date": "2022-01-25",
"channel": "Teste1",
"campaign": "teste1",
},
"received": 41683,
"enqueued": 0,
},
{
"_id": {
"uuid": "1",
"date": "2022-01-26",
"channel": "Teste2",
"campaign": "teste2",
},
"received": 314670,
"enqueued": 0,
},
]
I want to create or destructing this array and create something like this:
"data": [
{
"_id": {
"uuid": "1",
"date": "2022-01-25",
"channel": "Teste1",
"campaign": "teste1",
},
},
{
"_id": {
"uuid": "2",
"date": "2022-01-26",
"channel": "Teste2",
"campaign": "teste2",
},
},
]
How can I copy it? how can I use destructuring on this array?
Solution 1:[1]
Using map() you can
const data = [
{
"_id": { "uuid": "1", "date": "2022-01-25", "channel": "Teste", "campaign": "teste" },
"received": 41683,
"enqueued": 0,
},
{
"_id": { "uuid": "1", "date": "2022-01-25", "channel": "Teste", "campaign": "teste"},
"received": 314670,
"enqueued": 0,
},
];
let result = data.map(e =>{
return { _id: e._id }
})
console.log(result);
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 | Nitheesh |
