js toma varias claves y valores de un objeto para formar un objeto de matriz

   var obj = {
      amount_after_discount: 4,
      brand_id: "153",
      cid: "4654",
      coupon_id: "0,0",
      discount: 0,
      freight: "107",
      freight_price: "20",
      is_distribution: "0",
      mch_id: "154",
      membership_price: 4,
      num: "1",
      pid: "912",
      price: "400.00",
    }
    //想分别获取它的key 和 value

    //小技巧来啦

    let list = [];
    for (var key in obj) {
      var temp = {}
      if (key == "cid") {
        temp.cid = obj[key]
        list.push(temp)
      } if (key == 'pid') {
        temp.pid = obj[key]
        list.push(temp)
      } if (key == 'num') {
        temp.num = obj[key]
        list.push(temp)
      }

      // temp.key= obj[key];
    }
    console.log(list, "fhiudfids")

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/weixin_45663264/article/details/110823879
Recomendado
Clasificación