0
我有一个对象,作为一个ID作为密钥和对象值的阵列,我需要生成从该操纵对象值,并产生一个新的对象
的目标的矩阵
var bears = {
"1" :
[
{ 'total_bears': 2, 'bear_id': 1, 'location': 'CA' },
{ 'bear_age': 100, 'bear_id': 1, 'location': 'CA' },
{ 'total_bears': 1, 'bear_id': 1, 'location': 'NM' },
{ 'bear_age': 10, 'bear_id': 1, 'location': 'NM' }
],
"2" :
[
{ 'total_bears': 1, 'bear_id': 2, 'location': 'CA' },
{ 'bear_age': 50, 'bear_id': 2, 'location': 'CA' }
]
};
结果
{ 'bear_id' : 1, 'locationCAtotal_bears' : 2, 'locationCAbear_age': 100, 'locationNMtotal_bears': 1, 'locationNMbear_age': 100}
{ 'bear_id' : 2, 'locationCAtotal_bears' : 1, 'locationCAbear_age': 50}
我有什么
for (var key in bears) {
var arr = bears[key];
var obj = {};
var new_arr = arr.map(function(item) {
if (item.location == 'CA') {
obj.bear_id = item.bear_id;
obj.locationCAtotal_bears = item.total_bears;
obj.locationCAbear_age = item.bear_age;
}
else if (item.location == 'NM') {
obj.bear_id = item.bear_id;
obj.locationNMtotal_bears = item.total_bears;
obj.locationNMbear_age = item.bear_age;
}
return obj
});
}
这是我尝试过的许多代码之一。我已经尝试了不同的东西就可以了,但仍然没有运气
请向我们提供您迄今为止所做的工作。然后,我们将能够指出你的错误 –
@NafiulIslam我已经添加了我写的代码 – akinjide