因此_.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' } } ]
*/