0

This one has always served me well...

var myProperty = "FOO"

var expenseSelect = expenseArray.filter(function(obj){
    return obj.property == myProperty
}); 

But now I have a situation where the obj.property is an array of properties ["FOO", "BAR", "WEE"] inside the expenseArray.

Is there a smart way to do this? Or do I have to do the whole loop inside loop thing?

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53

2 Answers2

2

If you want to check if myProperty is in the array you can do it using

var myProperty = "FOO"

var expenseSelect = expenseArray.filter(function(obj){
    return obj.property.includes(myProperty);
}); 
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

using the some() method tests whether at-least one element in the array passes the test implemented by the provided function, it can be a simple an option..

var myProperty = "FOO";
var expenseArray=[];
expenseArray[0]={ property: ["FOO", "BAR", "WEE"] };
expenseArray[1]={ property: ["NoFOO", "BAR", "WEE"]} ;

var expenseSelect = expenseArray.filter(function(obj){
    return obj.property.some(function(element,index,array){
      return element == myProperty;
    });
});

console.log(expenseSelect);
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37