Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How to insert each array item into each object of other array?

I have 2 arrays

I want this:

[{name: 'Sweater', qty: 1}, {name: 'Skirt', qty: 3}, {name: 'Socks', qty: 2}]

but I get

    qtyList = [1, 3, 2]
    data = [{name: 'Sweater', qty: 1}, {name: 'Skirt', qty: 1}, {name: 'Socks', qty: 1}]

    let assignQty = data.map((x, id) => {
            qtyList.map((y, idx) => {
                if (idx == id) return x.qty = y
            })
        })
        console.log('assignQty', assignQty)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You're essentially zipping the 2 lists together - typically done as follows:

const qtyList = [1, 3, 2]
const data = [{name: 'Sweater', qty: 1}, {name: 'Skirt', qty: 1}, {name: 'Socks', qty: 1}]

const result = data.map((item,idx) => ({...item, qty: qtyList[idx]}))

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Without using the spread operator, update the required field and then return x.

    qtyList = [1, 3, 2]
    data = [{name: 'Sweater', qty: 1}, {name: 'Skirt', qty: 1}, {name: 'Socks', qty: 1}]

    var assignQty = data.map((x, id) => {x.qty=qtyList[id];return x;})

    console.log('assignQty', assignQty)

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is my approach to your question. In your example, you were trying to map two arrays into one. This approach "loops" the data array containing the objects you want to change.

Using the index of each object it sets the item.qty to the value in qtyList[index].

    qtyList = [1, 3, 2];
    data = [{
      name: 'Sweater',
      qty: 1
    }, {
      name: 'Skirt',
      qty: 1
    }, {
      name: 'Socks',
      qty: 1
    }];

    let assignQty = data.map((item, index) => {
      item.qty = qtyList[index];
      return item;
    });
    console.log(assignQty)

about 4 years 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 vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda