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?
Array.map
through the array, return only _id
node of each object
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,
},
];
const output = data.map((item) => {
const { _id } = item;
return { _id }
});
console.log(output);
OR Simply select _id
node and return it as below.
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,
},
];
const output = data.map(({ _id }) => ({ _id }));
console.log(output);
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);