简体   繁体   English

我如何比较Array.find和foreach等迭代器?

[英]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 ),例如: forEachmapfilterreducereduceRightsomeeveryfindfindIntex

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 . forEachfor相比, find函数的性能有所不同,可以为O(1)O(n)或更小,因为forEachfor复杂度始终为 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.

相关问题 如何使用 Array.find() 函数来编辑特定对象内的名称和年龄属性 - how can I use Array.find() function to edit name and age properties inside specific object 如何在 Javascript 中使用循环(forEach、map 等)将数组转换为对象 - How to convert array to object using loop (forEach, map, etc..) in Javascript 量角器-是否可以将$$(elementLocator).first()用于更多元素,例如第二,第三等等。 - Protractor - Can I use $$(elementLocator).first() for more elements like the second, third and etc..? 如何优化多个 Array.find() - how to optimize multiple Array.find() if Array.find是否有简写形式? - Is there a shorthand for `if Array.find`? 什么时候将在jQuery,mootools等库中构建JavaScript引擎(如Spidermonkey),并且有人可以帮助实现这一点? - When is a javascript engine (like Spidermonkey) going to build in a library like jquery, mootools, etc.. and how can anyone help this to come about? 我如何调用HTML DOM事件对象(例如:onclick,onblur等)的嵌套函数? - How can I call to HTML DOM Event Object (for example: onclick, onblur, etc..) nested functions? 如何在 TextBox(用户、密码等)中缩进文本和光标的起始位置 - How can I indent the text, and starting position of cursor, in a TextBox (user, password, etc..) Array.find() 正在改变对象? - Array.find() is changing objects? Array.find不是函数错误 - Array.find is not a function error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM