2017-01-27 8 views

答えて

1

gmarker[i].pricefirsthour = 2は、条件が、2と評価さ(と常にのでtruthyある割り当てではありません)。 ==または===が必要で、=ではありません。

代わりに、新しいJSの機能を備えた、短い:@

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

Rockafella-も、それらはECMAScriptの2015年に導入された矢印の機能を持つように注意し、あなたがそのような任意のよう(気にすべてのブラウザでサポートされない場合がありますバージョンの場合はIE)、[* MDNサポートマトリックス*](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions)を参照してください。 – RobG

1

使用Array.prototype.filter

gmarker = gmarker.filter(function(o){ 
    return o.pricefirsthour == 2; 
}); 

var gmarker = [{pricefirsthour:2},{pricefirsthour:21},{pricefirsthour:2},{pricefirsthour:7},{pricefirsthour:5},{pricefirsthour:2}]; 
 
gmarker = gmarker.filter(function(o){ 
 
    return o.pricefirsthour == 2; 
 
}); 
 
console.log(gmarker);

関連する問題