Underscore.js 使用下划线从对象创建数组
我有以下json作为示例Underscore.js 使用下划线从对象创建数组,underscore.js,Underscore.js,我有以下json作为示例 var employee=[{sex:'M',id:1},{sex:'M',id:3},{sex:'f',id:4},{sex:'f',id:5}] 我想要下面的数组 maleIds=[1,3] femaleIds=[4,5] var testFilter=_.filter(employee,function(obj) { return obj.sex=='M'; }); var t
var employee=[{sex:'M',id:1},{sex:'M',id:3},{sex:'f',id:4},{sex:'f',id:5}]
我想要下面的数组
maleIds=[1,3]
femaleIds=[4,5]
var testFilter=_.filter(employee,function(obj) {
return obj.sex=='M';
});
var testMap=_.map(testFilter, function(value, key){
return { name : key, value : value };
});
如何使用特定条件从对象创建数组
_过滤器/_映射它们返回整个对象。我只想要性别值。首先是员工数据。这将返回一个包含2个数组的数组;第一个数组包含所有雄性,第二个数组包含雌性。然后在分区数据上使用以获取ID:
var employee=[{sex:'M',id:1},{sex:'M',id:3},{sex:'f',id:4},{sex:'f',id:5}]
var partitions = _.partition(employee, {sex: 'M'})
var maleIds = _.pluck(partitions[0], 'id');
var femaleIds = _.pluck(partitions[1], 'id');
var employee=[{性别:'M',id:1},{性别:'M',id:3},{性别:'f',id:4},{性别:'f',id:5}]
var partitions=\分区(雇员,{sex:'M'})
var-maleIds=u.pull(分区[0],'id');
var femaleIds=u2;.pull(分区[1],'id');
document.getElementById('males').textContent=JSON.stringify(maleIds);
document.getElementById('femaleIds').textContent=JSON.stringify(femaleIds)代码>
男性
女性
非常感谢。我是一个新来强调的人,项目时间限制并没有让我学习整个文档。