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

288
Visualizações
How to push all the objects inside an array

It will give data by checking for each variant inside the variant array, it show all the object if the condition matched or undefined if not, but in this particular code, it is creating a new array for each item, something like this :- [{id: 'something'}] [{id: 'something'}] [{id: 'something'}] [{id: 'something'}] I want it to have all the result inside one array:-

[
  {id: 'something'},
  {id: 'something'},
  {id: 'something'},
  {id: 'something'}
]
const mynewarr = [];
const myimages = product_details.data.product.images;
      
      for(var i = 0; i < product_details.data.product.variants.length; i++) {
        const myvari = product_details.data.product.variants[i].image_id;
        const real = myimages.find(imageid => imageid.id == myvari);
        mynewarr.push(real);
        }
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Just use a destructuring assignment to "unpack" the array before the push.

mynewarr.push(...real);
about 4 years ago · Santiago Trujillo Relatório

0

If I understand correctly you need to go:

const newArray = product_details.data.product.variants.map((variant) => variant.image_id === 'your_condition' ? { id: variant.image_id } : undefined)

The newArray will contain an array of objects with the ids.

about 4 years ago · Santiago Trujillo Relatório

0

Since your myimages is an array of array, you can push the object by accessing the 0th index of the filtered item(using .find). If there's a possibility that the .find method might return undefined you can add a conditional check to push only the found items. You can update your code to something like the below.

      const mynewarr = [];
      const myimages = product_details.data.product.images;
      
      for(var i = 0; i < product_details.data.product.variants.length; i++){
        const myvari = product_details.data.product.variants[i].image_id;
        const real = myimages.find(imageid => imageid.id === myvari) 
        if(real){
         mynewarr.push(real[0]); //Add only the object and not the sub array.
       }
      }
about 4 years ago · Santiago Trujillo 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