【问题标题】:Filter an array of objects by the length of the value of a string按字符串值的长度过滤对象数组
【发布时间】:2018-11-27 21:10:29
【问题描述】:

想要通过键的数组值过滤对象数组。

var data = [
  {
    "name": "Jim",
    "age" : []
  },
  {
    "name": "Jerry",
    "age": [1,2]
  }
];
 var notEmpty = _.filter(data, ['age', null]);

希望变量notEmpty 的输出返回对象 Jerry,因为年龄数组不为空。

【问题讨论】:

    标签: javascript arrays loops filter lodash


    【解决方案1】:

    如果没有 lodash,它将是:

      const notEmpty = data.filter(el => el.age.length);
    

    【讨论】:

      【解决方案2】:

      使用 vanilla javascript,这将给出您想要的结果:

      data.filter(x => x["age"].length > 0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 2019-04-04
        • 2021-12-15
        相关资源
        最近更新 更多