为了账号安全,请及时绑定邮箱和手机立即绑定

多个对象怎么组合成一个数组对象

多个对象怎么组合成一个数组对象

慕丝7291255 2019-05-22 17:06:04
{"date":{"0":"20180201","1":"20180202","2":"20180205"},"lines":{"0":3.3,"1":10.4,"2":79.721},"count":{"0":5,"1":21,"2":13},"counts":{"0":5,"1":26,"2":39},"limit":{"0":3.3,"1":13.7,"2":93.421}}//最后我想变成下面这种格式,怎么弄呢letvalues=[{date:'20180201',lines:3.3,count:5,countS:5,limit:3.3},{date:'20180202',lines:10.4,count:21,countS:26,limit:13.7}...];
查看完整描述

2 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

letarr=[]
for(letkeyinobj){
arr.push(obj[key])
}
console.log(arr)
                            
查看完整回答
反对 回复 2019-05-22
?
交互式爱情

TA贡献1712条经验 获得超3个赞

constobj={
"date":{"0":"20180201","1":"20180202","2":"20180205"},
"lines":{"0":3.3,"1":10.4,"2":79.721},
"count":{"0":5,"1":21,"2":13},
"counts":{"0":5,"1":26,"2":39},
"limit":{"0":3.3,"1":13.7,"2":93.421}
};
constkeys=Object.keys(obj);
constlens=Object.keys(obj[keys[0]]);
letarrays=[]
for(constlenoflens){
leto={};
for(constkeyofkeys){
o[key]=obj[key][len];
}
arrays.push(o);
}
console.log(arrays);
结果:
[{date:'20180201',lines:3.3,count:5,counts:5,limit:3.3},
{date:'20180202',
lines:10.4,
count:21,
counts:26,
limit:13.7},
{date:'20180205',
lines:79.721,
count:13,
counts:39,
limit:93.421}]
新需求:
constobj={
"v_date":{"0":"20180201","1":"20180202","2":"20180205","3":"20180206","4":"20180207","5":"20180208"},
"type":{"0":"现券交易","1":"现券交易","2":"质押式回购","3":"质押式回购","4":"质押式回购","5":"现券交易"},
"amt":{"0":3.3,"1":10.4,"2":79.721,"3":68.363,"4":25.674,"5":12.1},
"num":{"0":5,"1":21,"2":13,"3":21,"4":18,"5":32},
"sum_num":{"0":5,"1":26,"2":13,"3":34,"4":52,"5":58},
"sum_amt":{"0":3.3,"1":13.7,"2":79.721,"3":148.084,"4":173.758,"5":25.8}
};
constkeys=Object.keys(obj);
constlens=Object.keys(obj.type);
lettypes={};
for(consttoflens){
if(!types[obj.type[t]]){
types[obj.type[t]]=[];
}
leto={};
for(constkeyofkeys){
if(key!=='type'){
o[key]=obj[key][t]
}
}
types[obj.type[t]].push(o);
}
console.log(types)
结果:
{'现券交易':
[{v_date:'20180201',amt:3.3,num:5,sum_num:5,sum_amt:3.3},
{v_date:'20180202',
amt:10.4,
num:21,
sum_num:26,
sum_amt:13.7},
{v_date:'20180208',
amt:12.1,
num:32,
sum_num:58,
sum_amt:25.8}],
'质押式回购':
[{v_date:'20180205',
amt:79.721,
num:13,
sum_num:13,
sum_amt:79.721},
{v_date:'20180206',
amt:68.363,
num:21,
sum_num:34,
sum_amt:148.084},
{v_date:'20180207',
amt:25.674,
num:18,
sum_num:52,
sum_amt:173.758}]}
当你执行console.log(Object.values(types))的时候,你会发现更多的惊喜
                            
查看完整回答
反对 回复 2019-05-22
  • 2 回答
  • 0 关注
  • 1799 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信