[英]How can i compare Array.find with iterators like for foreach etc..?
I regularly use for
loop , forEach
loop , Array.map
etc for iterating arrays. 我经常使用
for
loop, forEach
loop, Array.map
等来迭代数组。 i came to know that we can also use Array.find
for looping array and it can also return the values matched in the array based on condition: 我知道我们也可以使用
Array.find
循环数组,并且它还可以根据条件返回数组中匹配的值:
I have iterated array of objects using Array.find()
and for
loop as follows 我使用
Array.find()
和for
循环迭代对象数组,如下所示
Using Array.find: 使用Array.find:
[{
name: "John"
}, {
name: "Cannady"
}, {
name: "Sherlock"
}].find(function(oArrayObject) {
console.log(oArrayObject)
});
output on console: 在控制台上输出:
{name: "John"}
{name: "Cannady"}
{name: "Sherlock"}
Using for loop: 使用for循环:
var aNames=[{
name: "John"
}, {
name: "Cannady"
}, {
name: "Sherlock"
}]
for(var i=-0;i<aNames.length;i++){
console.log(aNames[i]);
}
output on console: 在控制台上输出:
{name: "John"}
{name: "Cannady"}
{name: "Sherlock"}
Both codes did the same thing. 两种代码都做同样的事情。
What is the advantage of Array.find() over for loop?? Array.find()与for循环相比有什么优点?
Which one gives the better performance ?? 哪个提供了更好的性能?
Please explain me the difference between array.find and other iterators in terms of performance , usage , internal functionality etc... 请在性能,用法,内部功能等方面向我说明array.find和其他迭代器之间的区别...
Array#find
is used to, well, find an item in the array that matches a predicate, and return it. Array#find
用于查找数组中与谓词匹配的项,然后将其返回。 When Array.find
returns, the loop ends. 当
Array.find
返回时,循环结束。 Performance wise, you don't have to iterate the whole array, if the item you seek is not the last. 在性能方面,如果要查找的项不是最后一个,则不必迭代整个数组。
var result = [{ name: "John" }, { name: "Cannady" }, { name: "Sherlock" }].find(function(o) { return o.name.startsWith('Ca'); }); console.log(result);
There are many methods to iterate arrays ( see array in mdn ), such as: forEach
, map
, filter
, reduce
, reduceRight
, some
, every
, find
, and findIntex
. 有很多迭代数组的方法( 请参阅mdn中的array ),例如:
forEach
, map
, filter
, reduce
, reduceRight
, some
, every
, find
和findIntex
。
Except for Array#forEach
, most of them provide additional functionality beyond the iteration itself. 除了
Array#forEach
,它们中的大多数都提供了迭代本身之外的其他功能。
The find() method returns a value of the first element in the array that satisfies the provided testing function.
find()方法返回满足提供的测试功能的数组中第一个元素的值。 Otherwise undefined is returned.
否则返回undefined。
find
function use a callback function, for example: find
函数使用回调函数,例如:
function isBigEnough(element) {
return element >= 15;
}
[12, 5, 8, 130, 44].find(isBigEnough);
The find method executes the callback function once for each element present in the array until it finds one where callback returns a true value. find方法对数组中存在的每个元素执行一次回调函数,直到找到其中回调返回真值的元素为止。 If such an element is found, find immediately returns the value of that element.
如果找到这样的元素,则find立即返回该元素的值。
The performance of find
function varies, can be O(1)
,or O(n)
or less,in contrast to the forEach
and for
,where complexity is always O(n)
because you have to iterate all the array
. 与
forEach
和for
相比, find
函数的性能有所不同,可以为O(1)
或O(n)
或更小,因为forEach
和for
复杂度始终为 O(n)
因为您必须迭代所有array
。
Using for/forEach for iteration is when you need to touch or check every object in the collection. 当需要触摸或检查集合中的每个对象时,使用for / forEach进行迭代。 The find method is when you know what you're looking for and just want a quick search through the collection to see if it's there.
find方法是当您知道要查找的内容时,只需要快速搜索集合以查看是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.