因此,下劃線中的_.map()
函數不返回對象,但它需要它們。有沒有什麼辦法讓它返回完全相同的對象?從_.map()返回對象
var _ = require("underscore");
var cars = {
"mom": {
"miles": "6",
"gas": "4"
},
"dad": {
"miles": "6",
"gas": "4"
}
}
var regurgitate_cars = _.map(cars, function(value, key){
return value;
});
/*
[ { miles: '6', gas: '4' }, { miles: '6', gas: '4' } ]
*/
var regurgitate_cars = _.map(cars, function(value, key){
var transfer = {};
transfer[key] = value;
return transfer;
});
/*
[ { mom: { miles: '6', gas: '4' } },
{ dad: { miles: '6', gas: '4' } } ]
*/
你想達到什麼目的?淺/深複製一個對象? –
查看地圖價值的下劃線https://github.com/jashkenas/underscore/issues/220 – megawac