【问题标题】:how to use match function in angularJS to filter out an object with certain string value如何在angularJS中使用match函数过滤出具有特定字符串值的对象
【发布时间】:2017-04-05 18:33:06
【问题描述】:

我正在尝试过滤掉文件名值为“foo”的任何对象

这是我目前所拥有的

          var main_item = [];
          var child_item = [];
          angular.forEach(item.files, function(value, key){             
            if(value.indicator === true && value.fileName.match("foo")) {
              main_item.push(value);
            } else {
              child_item.push(value);
            } 
          });

所以如果 item.files.indicator 为 true 并且 item.fileName 匹配 "foo" 然后推送到 main_item 数组

【问题讨论】:

  • 试试 Array.prototype.filter()
  • 您已将item 定义为一个空数组,然后两行之后您尝试对其进行迭代。我不认为你会得到你期望的结果。
  • @Lex 虽然我很欣赏你的幽默感,但我认为他可能发布了一些伪解码,但我可能错了
  • 没错。关键是我们不知道没有minimal reproducible example
  • 是的,固定.......

标签: javascript angularjs foreach match


【解决方案1】:

item.files.filter(function(file) {return file.fileName == "foo"})

可能是您正在寻找的东西

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多