underscore.js,遍历数组以列出特定值的所有索引

时间:2015-09-22 10:54:04

标签: underscore.js

我有一个数组,当我找到一个键的真值时,我试图迭代以获取所有索引

var f = new FileInfo(filePath);
{P:\poolman\LY21\2015\LY21_2015-03-25_03.xml}
    base: {P:\poolman\LY21\2015\LY21_2015-03-25_03.xml}
    _name: "LY21_2015-03-25_03.xml"
    Directory: {P:\poolman\LY21\2015}
    DirectoryName: "P:\\poolman\\LY21\\2015"
    Exists: false
    IsReadOnly: true
    Length: '(var f = new FileInfo(filePath);).Length' threw an exception of type 'System.IO.FileNotFoundException'
    Name: "LY21_2015-03-25_03.xml"

我只能获得第一个索引,而不是全部(我应该得到[0,2]

    sheet.headings = [
      {
        "label": "SheetId",
        "access": "R",
        "hidden": true,
        "position": "left",
        "input_type": "text"
      },
      {
        "label": "Moteur Affichage",
        "access": "W",
        "hidden": false,
        "position": "left",
        "input_type": "text",
        "value_type": "text"
      },
      {
        "label": "Navigateur",
        "access": "W",
        "hidden": true,
        "position": "left",
        "input_type": "text",
        "value_type": "text"
      }
    ]

1 个答案:

答案 0 :(得分:1)

也许你可以使用Array的map和filter函数来实现这一点。类似的东西:

sheet.headings.map(function(e,i){
    if (e.hidden) return i;
}).filter(function(e){
    return typeof e != 'undefined';
})

首先你得到一个索引的数组,而不是匹配hidden == true,然后你过滤删除未定义的对象。