我目前的收藏如下:
[
{Tom:true,age:23},
{Jack:true:age:22},
{Lucy:false,age:22}
]
如何通过下划线将其转换为此内容?
[
{name:'Tom',isMale:true,age:23},
{name:'Jack',isMale:true,age:22},
{name:'Lucy',isMake:true,age:22}
]
答案 0 :(得分:2)
您可以使用reduce
(瑞士军刀迭代器)复制内部对象,同时重新排列name
和isMale
值。像这样:
_(a).map(function(o) {
return _(o).reduce(function(memo, v, k) {
if(k === 'age') {
# Copy 'age' through as-is
memo[k] = v
}
else {
# Assume the rest is name:boolean so bust it apart
memo.name = k;
memo.isMale = v;
}
return memo;
}, { });
});
答案 1 :(得分:1)