我有一个对象数组。我想groupby
& filter(remove)
对象,未定义的类别。请参阅下面的脚本以获得更清晰:
arrayFlatten = [
{
area:"Digital",
category:undefined,
qId:"q11",
qqId:"step1",
type:"Reduce",
val:1,
userId:1,
weightedAverage:0
},
{
area:"Digital",
category:"Analytics",
qId:"q1",
qqId:"step1",
type:"Reduce",
val:1,
userId:1,
weightedAverage:0
},
{
area:"Digital",
category:"Analytics",
qId:"q1",
qqId:"step2",
type:"Reduce",
val:1,
userId:1,
weightedAverage:0
}
]
用于按
分组的命令var groupCategory = _.groupBy(arrayFlatten,'category');
console.log(groupCategory);
预期结果应该如下:
arrayFlatten = [
{
area:"Digital",
category:"Analytics",
qId:"q1",
qqId:"step1",
type:"Reduce",
val:1,
userId:1,
weightedAverage:0
},
{
area:"Digital",
category:"Analytics",
qId:"q1",
qqId:"step2",
type:"Reduce",
val:1,
userId:1,
weightedAverage:0
}
]
答案 0 :(得分:1)
filteredArray = _.filter(
arrayFlatten,
function (obj) {
return obj.category !== undefined;
}
);
filteredArray
将包含没有未定义类别的数组。
您也可以使用此处所述的Array.filter()
在大多数现代浏览器中不使用下划线执行此操作:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter