Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

46
Vistas
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?

7 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

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

7 months ago · Juan Pablo Isaza Denunciar

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

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.