Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push all the objects inside an array

Tags:

javascript

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);
        }
like image 350
Leith Avatar asked Nov 28 '25 02:11

Leith


1 Answers

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

mynewarr.push(...real);
like image 100
waken22 Avatar answered Nov 29 '25 14:11

waken22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!