js takes several keys and values from an object to form an array object

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

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45663264/article/details/110823879