如何输出产品ID,价格和数量的数组

时间:2019-01-11 16:06:27

标签: javascript arrays

需要返回一个数组,该数组的表中的数据包含产品ID,用户交易的单价和数量的列表。格式如下:

[{ 
  'id':'#productID1#', 
  'price':'#productPrice1#',
  'quantity':'#productQty1#'
 },
 {
  'id':'#productID2#',
  'price':'#productPrice2#', 
  'quantity':'#productQty2#'
 }]

试图根据需要使用引号添加标题(产品,价格和数量)以及撇号。但是,只要有撇号且无法在变量的开头和结尾处获得所需的格式,我都会收到\。这是我输出的内容:

'id\':\'WE119017\',\'price\':\'undefined\',\'quantity\':\'undefined,
 id\':\'WE119012\',\'price\':\'undefined\',\'quantity\':\'undefined,
 id\':\'858191\',\'price\':\'undefined\',\'quantity\':\'undefined,
 id\':\'WE108003\',\'price\':\'undefined\',\'quantity\':\'undefined,
 id\':\'854220\',\'price\':\'undefined\',\'quantity\':\'undefined'

任何帮助将不胜感激,因为我没有Java知识或经验,并且正在尝试使用在线资源来解决这个问题。谢谢!

    function() {
    var products = {{Ecommerce Imp Object}};
    return products.reduce(function(arr, prod)
    { return arr.concat("id':'" + prod.id + "'," + "'price':'" + prod.price + "'," + "'quantity'"+":'" + prod.quantity); }, []).join(',');
    }

0 个答案:

没有答案