Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
How to insert a different object with an array inside an array

I'm trying to test something in javascript with an array and object here is what I'm trying to do:

CODE:

    const data = [
      {
        name: "Order A",
        orderItems: [
          {
            qty: [2, 2, 2],
            products: [
              { name: "Product A" },
              { name: "Product B" },
              { name: "Product C" },
            ],
          },
        ],
      },
    ];
    
    const dataName = data.map(({ name }) => {
      return name;
    });
    
    const product = data.map(({ orderItems }) => {
      return orderItems.map(({ qty, products }) => {
        return products.map(({ name }) => {
          return name + qty;
        });
      });
    });
    
    console.log(`This is ${dataName} with ${product}`);

I want it to return something like A2, B2, and C2 but it returns something like A2,2,2 B2,2,2 C2,2,2 I know I did something wrong. If you need anymore clarification please let me know.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're concatenating the whole qty array. Pass the products array index and concatenate the corresponding qty instead.

const product = data.map(({ orderItems }) => {
  return orderItems.map(({ qty, products }) => {
    return products.map(({ name }, i) => {
      return name + qty[i];
    });
  });
});

console.log(`This is ${dataName} with ${product}`);

Output

This is Order A with Product A2,Product B2,Product C2
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this? Don't forget you've got an index as the second param of array methods, and i think - you just need to use it to solve your problem :)

 const ret = data.flatMap(({orderItems}) => orderItems).flatMap(({qty, products}) => {
    return qty.flatMap((quantity, index) => ({quantity, name: products[index].name}))
 });
ret.forEach(({name, quantity})=>console.log(name, quantity))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda