如果找到three
,那麼它應該返回true並停止迭代。否則返回false,如果沒有找到。如何在發現後停止循環?
我使用filter()
- 是錯誤的做法使用?
var data = [
'one',
'two',
'three',
'four',
'three',
'five',
];
found = data.filter(function(x) {
console.log(x);
return x == "three";
});
console.log(found);
演示:https://jsbin.com/dimolimayi/edit?js,console
'濾波器()'方法,運行於**數組中的每一項**該給予功能,並返回該函數返回true的所有項目的陣列; 有用的鏈接:https://coderwall.com/p/_ggh2w/the-array-native-every-filter-map-some-foreach-methods –
沒有錯,使用'filter',但你可以使用['。 find()'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)以滿足您的要求... http://stackoverflow.com /問題/ 23614054/JavaScript的細微差別 - 的 - myArray的-的foreach-VS-for循環 – Mottie