我在JavaScript中有var x
var x = [{"id":"11","affiliate_id":"190","affiliate_percent":"0.2","read_only":"1"},{"id":"12","affiliate_id":"33","affiliate_percent":"0.1","read_only":"1"}];
我想要价值:
var x = [{"190":"0.2"},{"33":"0.1"}];
此处affiliate_id
值为关键,affiliate_percent
值为值。
答案 0 :(得分:3)
试试:
var x = [
{"id":"11","affiliate_id":"190","affiliate_percent":"0.2","read_only":"1"},
{"id":"12","affiliate_id":"33","affiliate_percent":"0.1","read_only":"1"}
];
var output = [];
for (var key in x) {
var item = {};
item[x[key].affiliate_id] = x[key].affiliate_percent;
output.push(item);
}
输出:
[{"190":"0.2"},{"33":"0.1"}]
答案 1 :(得分:-1)
如果您尝试动态创建新的值键,则可以执行此操作:
var obj={};
obj[affiliate_id] = ""[{"id":"11","affiliate_id":"190","affiliate_percent":"0.2","read_only":"1"},{"id":"12","affiliate_id":"33","affiliate_percent":"0.1","read_only":"1"}]";