JavaScript 多个数组对象

JavaScript multiple array objects

本文关键字:对象 数组 JavaScript      更新时间:2023-09-26

我想用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
         }]
     }
   }
});

.push() 语句的末尾有几个额外的大括号。试试这个:

<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>