我有一个对象数组。我想groupbyfilter(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
          }
    ]

最佳答案

filteredArray = _.filter(
    arrayFlatten,
    function (obj) {
        return obj.category !== undefined;
    }
);


filteredArray将包含没有未定义类别的数组。

您也可以在大多数现代浏览器中使用以下描述的Array.filter()在不加下划线的情况下执行此操作:

10-06 02:51