简体   繁体   中英

JavaScript multiple array objects

I would like to create this array in Javascript, but i am actually struck in the process. I have given an example below:

I am facing a very big problem in generating the second array of the 'product' list object. Could any of you please advise me how I can generate multiple product lists like what has been done below.

var dataLayer = [];
dataLayer.push({
         'products': [{
             'name': 'FTP',
             'id': 'TXN_987666',
             'price': 123,
             'brand': 'ABC',
             'category': 'RTP',
             'quantity': '4'
         }, {
             'name': 'CHI',
             'id': 'TXN_89798798',
             'price': 656,
             'brand': 'Fairmont',
             'category': 'FMP',
             'quantity': 7
         }]
     }
   }
});

There are a couple of extra braces at the end of your .push() statement. Try this:

 <script type="text/javascript"> var dataLayer = []; dataLayer.push({ 'products': [{ 'name': 'FTP', 'id': 'TXN_987666', 'price': 123, 'brand': 'ABC', 'category': 'RTP', 'quantity': '4' }, { 'name': 'CHI', 'id': 'TXN_89798798', 'price': 656, 'brand': 'Fairmont', 'category': 'FMP', 'quantity': 7 }] }); // Test the object contents -- // Click arrows to expand the array members in the console console.log(dataLayer); </script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM