Difference:

find()

  1. If condition is met: Returns the condition in the arrayValue of the first element(subsequent values do not call the execution function);
  2. If the condition is not met: Returnsundefined;

findIndex()

  1. If condition is met: Returns the condition in the arrayThe first element position(subsequent values do not call the execution function);
  2. If the condition is not met: Returns- 1;

filter()

  1. If condition is met: Returns the condition in the arrayAll the elements. (subsequent values do not call the execution function);
  2. If the condition is not met: ReturnsAn empty array;

Similarities:

  1. Find (), findIndex(), filter() is not executed for an empty array;
  2. Do not change the original value of the array;
  3. Array. find(function(currentValue, index, arr),thisValue)
    CurrentValue: Required. This parameter is optional. Index of the current element Element index: Optional. Index arR of the current element: optional. ThisValue: Optional. The value passed to the function is usually the "this" value. If this parameter is empty, "undefined" is passed to the value "this"Copy the code

Code practices:

find()

Find (item => item > 3) // Find (item => item > 3) // Find (item => item > 3) // Find (item < 0) let resultb = Arr. Find (item => item < 0) let resultc = arr. Find ((value,index,arr)=>{return index == 3}); console.log(resulta)//4 console.log(resultb)//undefined console.log(resultc)//1 console.log(arr)//(9) [0, 1, 2, 1, 3, 4, 5, 6, 7)Copy the code

findIndex()

Const arr = [{id: 1, dis: "Monday"}, {id: 2, dis: "Tuesday"}, {id: 3, dis: "on Wednesday"}, {id: 4, dis: "Thursday"}, {id: 5, dis: "Friday"}); var result1=arr.findIndex((item)=>item.id==4); console.log(result1); // 3 var result2=arr.findIndex((item)=>item.id==100); console.log(result2); / / 1Copy the code

filter()

Const arr = [{id: 1, dis: "Monday"}, {id: 2, dis: "Tuesday"}, {id: 3, dis: "on Wednesday"}, {id: 4, dis: "Thursday"}, {id: 5, dis: "Friday"}); let a = arr.filter(item => item.id > 2); let b = arr.filter(item => item.id > 5); console.log(a); / / / {id: 3, dis: "on Wednesday"}, {id: 4, dis: "Thursday"}, {id: 5, dis: "Friday"}] the console. The log (b); / / []Copy the code

Article Reference:

https://blog.csdn.net/u012149969/article/details/82843652
https://www.runoob.com/jsref/jsref-find.html
https://www.runoob.com/jsref/jsref-findindex.html
https://www.runoob.com/jsref/jsref-filter.html
Copy the code