0

I have an array in which I am tryingg to iterate to get ALL indexes when a true value is found for a key

    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"
      }
    ]

I can only get the first index, not all of the ( I should get [0,2]

    var column_hidden = _.findIndex(sheet.headings, function(col) {
      return col.hidden === true ;
    });

1 Answer 1

1

Maybe you can achieve this using the map and filter functions of Array. Something like:

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

First you get an array with the index than matches hidden==true and then you filter to remove the objects that are undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

the 'ruby' way.. thanks a lot it's giving the expected result.... I was too narrow-minded, trying to use underscore.js functions....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.