js 从一个对象中取其中的几个key 和value 组成数组对象
作者:互联网
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")
标签:obj,temp,pid,list,value,js,中取,key,id 来源: https://blog.csdn.net/weixin_45663264/article/details/110823879