如果找到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