Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!