2016-06-11 61 views
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 
    }); 
} 

这是我尝试过的许多代码之一。我已经尝试了不同的东西就可以了,但仍然没有运气

+1

请向我们提供您迄今为止所做的工作。然后,我们将能够指出你的错误 –

+0

@NafiulIslam我已经添加了我写的代码 – akinjide

回答

1

使用Array#forEachArray#reduce方法

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' 
 
    }] 
 
}; 
 

 

 
var res = Object.keys(bears).map(function(k) { 
 
    return bears[k].reduce(function(obj, ele) { 
 
    Object.keys(ele).forEach(function(key) { 
 
     // set all property except location and bear//-id 
 
     if (key !== 'bear_id' && key !== 'location') 
 
     obj['location' + ele.location + key] = ele[key]; 
 
    }); 
 
    return obj; 
 
    // pass object with bear_id as initial value 
 
    }, { 
 
    bear_id: bears[k][0].bear_id 
 
    }); 
 
}); 
 

 
console.log(res);

0

工作的解决方案,而迭代的地图,对象(项目.total_bears)值是未定义的,你检查这样的条件。

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' } 
    ] 
    }; 

var newBears = []; 
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; 
    if (item.total_bears) 
     obj.location_CAtotal_bears = item.total_bears; 
    obj.locationCAbear_age = item.bear_age; 
    } 
    else if (item.location === 'NM') { 
    obj.bear_id = item.bear_id; 
    if (item.total_bears) 
     obj.location_NMtotal_bears = item.total_bears; 
    obj.locationNMbear_age = item.bear_age; 
    } 

    return obj 
    }); 
    newBears.push(obj); 
} 
console.log(JSON.stringify(newBears)); 

现在newBears数组会有你的结果。