Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between find and filter in es6

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "what are the differences between find and filter in es6", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the differences between find and filter in es6".

The difference between find and filter in es6: 1, when looking for qualified contents in the array without changing the array, the result returned by the find method is the object, and the result returned by the filter method is the array; 2, if there is no value to meet the test function, the find method returns an undefined, and the filter method returns an empty array.

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

What is the difference between find and filter in es6

1. Both find and filter are methods that do not change the original array, and both find the contents that meet the conditions in the array without changing the array. The difference is that find returns objects and filter returns arrays.

Examples are as follows:

Const list = [{'name':'1',index:1}, {' name':'2'}, {'name':'1'}] let list2 = list.find (I = > I. Nameplate examples examples 1') let list3 = list.filter (I = > I. Nameglasses examples examples 1') console.log (list); [{name:'1 indexname 1}, {name:'2'}, {name:'1'}] console.log (list2) {name: '1percent, index: 1} console.log (list3); [{name:' 1percent, index: 1}, {name:'1'}]

Find only finds the first qualified result, as in the example, an object is returned directly instead of an array! And filter returns that all the results are still arrays.

Note: find () will not traverse the following elements after finding the first element, so if there are two identical elements in the array, he will only find the first one and the second will not traverse again.

two。 To sum up, the query efficiency of find is more efficient, so it is best to use find if the data in the array is unique.

Find ()

The ES6 find () method returns the value of the first element that passes the test function. If no value satisfies the test function, undefined is returned.

Grammar

The arrow function used in the following syntax.

Find ((element) = > {/ *... * /}) find ((element, index) = > {/ *... * /}) find ((element, index, array) = > {/ *... * /})

Filter ()

The filter () method creates a new array containing all the elements that pass the test function. If no element satisfies the test function, an empty array is returned.

Grammar

Filter ((element) = > {/ *... * /}) filter ((element, index) = > {/ *... * /}) filter ((element, index, array) = > {/ *... * /})

Common point

Higher-order functions: both functions are higher-order functions.

Difference

1. Pass a test function

Find () returns the first element.

Filter () returns a new array of all elements that pass the test function.

2. If no value satisfies the test function

Find () returned undefined

Filter () returns an empty array

The above is about the content of this article on "what are the differences between find and filter in es6?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 271

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report