Search code examples
javascriptarrayspropertiesreturn

Return all arrays that meet property condition


I am trying to return all arrays that meet a condition which is property gmarker[i].pricefirsthour equal to 2. How can I achieve that? This below is just sample:

for (var i = 0; i < gmarker.length; i++) {
    if (gmarker[i].pricefirsthour = 2) {
        console.log("is equal to 2");
    };
}

Solution

  • gmarker[i].pricefirsthour = 2 is not a condition, but an assignment that evaluates to 2 (and is thus always truthy). You need == or ===, not =.

    Alternately, shorter, with newer JS features:

    gmarker.filter(x => x.pricefirsthour === 2)