代码之家  ›  专栏  ›  技术社区  ›  Nikhil Savaliya

从对象数组生成特定对象

  •  2
  • Nikhil Savaliya  · 技术社区  · 7 年前

    我有一个数组,

    var result = [
      {
        "sItem" : [
          "Pizza Margherita","Pizza marinara"
        ],
        "sImage" : [
       "https://assets.marthastewart.com/styles/wmax-300/d31/pizza-margherita-0606-mla102155/pizza-margherita-0606-mla102155_vert.jpg","https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"
        ],
        "nQuantity" : 1,
        "eDeliveryStatus" : "n",
        "nPrice" : 215,
        "sReceiptId" : "pRjZpGzIDPpX",
      }
    ];
    

    想使对象像,我正在运行一个循环通过标题数组推送数据。

    [  
       {  
          "title":"Pizza Margherita",
          "subtitle":"Pizza Margherita",
          "quantity":1,
          "price":215,
          "currency":"INR",
          "image_url":"https://images.mcdelivery.co.in/hardcastle-restaurants-pvt-ltd/image/upload/q_auto:low,fl_lossy,w_300/v1484907263/Items/2754.png"
       },
       {  
          "title":"Pizza marinara",
          "subtitle":"Pizza marinara",
          "quantity":1,
          "price":215,
          "currency":"INR",
          "image_url":"https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"
       }
    ]
    

    这就是我尝试失败的方式:

    result.forEach(el => {
      el.sItem.forEach(el2 => {
          elementRec.push({
              "title": el2,
              "subtitle": el2,
              "quantity": el.nQuantity,
              "price": el.nPrice,
              "currency": "INR",
              "image_url": el.sImage
          })
      });
    })
    

    我知道这是错误的,但对javascript来说是新的。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Nina Scholz    7 年前

    你可以绘制内部地图 sItem 以及相应的 sImage 通过一些破坏性和短暂的特性来制造新的物体。

    var data = [{ sItem: ["Pizza Margherita", "Pizza marinara"], sImage: ["https://assets.marthastewart.com/styles/wmax-300/d31/pizza-margherita-0606-mla102155/pizza-margherita-0606-mla102155_vert.jpg", "https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"], nQuantity: 1, eDeliveryStatus: "n", nPrice: 215, sReceiptId: "pRjZpGzIDPpX" }],
        result = data.reduce((r, { sItem, sImage, nQuantity: quantity, nPrice: price }) =>
            r.concat(sItem.map((title, i) => ({
                title, subTitle: title, quantity, price, currency: 'INR', image_url: sImage[i]
            }))),
            []
        );
    
    console.log(result);
        2
  •  2
  •   Guillaume Georges    7 年前

    你就快到了。

    在你身上 forEach ,添加 index 参数,并使用它从 sImage 数组:

    el.sItem.forEach((el2, index) => {
          elementRec.push({
              "title": el2,
              "subtitle": el2,
              "quantity": el.nQuantity,
              "price": el.nPrice,
              "currency": "INR",
              "image_url": el.sImage[index]
          })
      });
    

    var result = [
      {
        "sItem" : [
          "Pizza Margherita",
          "Pizza marinara"
        ],
        "sImage" : [
          "https://assets.marthastewart.com/styles/wmax-300/d31/pizza-margherita-0606-mla102155/pizza-margherita-0606-mla102155_vert.jpg",
          "https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"
        ],
        "nQuantity" : 1,
        "eDeliveryStatus" : "n",
        "nPrice" : 215,
        "sReceiptId" : "pRjZpGzIDPpX",
      }
    ];
    
    var elementRec = [];
    
    result.forEach(el => {
      el.sItem.forEach((el2, index) => {
          elementRec.push({
              "title": el2,
              "subtitle": el2,
              "quantity": el.nQuantity,
              "price": el.nPrice,
              "currency": "INR",
              "image_url": el.sImage[index]
          })
      });
    });
    
    console.log(elementRec);
        3
  •  -1
  •   mankowitz    7 年前

    我想你是想做这样的事。首先创建一个新数组。我称之为 converted .然后按 result 把东西放进去 .forEach() 这样地。

    var converted = [];
    result.forEach(function(i){
        converted.push({
    
      "title": i.sItem[0],
      "subtitle": i.sItem[0],
      "quantity": i.nQuantity,
      "price": i.nPrice,
      "currency": "INR",
      "image_url": i.sImage[0]
    
      });
    })
    

    试试这个 fiddle