This is the second day of my participation in the August More Text Challenge

methods concerns The return value JavaScriptversion
filter() All values that satisfy the condition Returns an array containing all elements that match the criteria. An empty array is returned if no element matches the criteria. 1.6
find() The first value that satisfies the condition Returns the value of the first array element that matches the test criteria, or if noneundefined. ECMAScript 6
findIndex() The position of the first value that satisfies the condition Returns the index of the first array element that matches the test criteria, or if none- 1. ECMAScript 6

ES5Find an array element in

The filter() method creates a new array of all the elements of the test implemented by the provided function.

  • filter()Empty arrays are not checked;
  • filter()Does not change the original array;
  • Grammar:array.filter(function(currentValue, index, arr), thisValue)
    • function(currentValue, index, arr): you must. Function, which is executed by each element in the array
      • currentValue: you must. The value of the current element
      • index: optional. The index value of the current element
      • arr: optional. The array object to which the current element belongs
    • thisValue: optional. Object is used when the callback is executed, passed to the function, and used asthisThe value of the. If you omitthisValue.thisThe value ofundefined.
var array = [1.2.3.4.5]
var find1 = array.filter(function (item) { // array.prototype.filter () returns an Array
  return item === 3
})
console.log(find1) / / [3]

var find2 = array.filter(function (item) { // array.prototype.filter () returns an Array
  return item === 6
})
console.log(find2) / / []

var find3 = array.filter(function (item) { // array.prototype.filter () returns an Array
  return item % 2= = =0
})
console.log(find3) / / (2, 4]
Copy the code

The disadvantage of the filter() function is that it checks all the elements in the array, which can affect performance (sometimes we just want to know if there is an element in the array, so we don’t need to go further).

ES6Find an array element in

1. Array.prototype.find()

The find() method returns the value of the first element in the array that satisfies the provided test function. Otherwise return undefined.

let array = [1.2.3.4.5.3]

let find1 = array.find(function (item) { // array.prototype.find () returns a value
  return item === 3
})
console.log(find1) / / 3

let find2 = array.find(function (item) { // array.prototype.find () returns a value
  return item === 31
})
console.log(find2) // undefined

let find3 = array.find(function (item) { // array.prototype.find () returns a value
  return item % 2= = =0
})
console.log(find3) / / 2
Copy the code

2. Array.prototype.findIndex()

The findIndex() method returns the index of the first element in the array that satisfies the provided test function. Returns -1 if no corresponding element is found.

let array = [1.2.3.4.5.3]
let find = array.findIndex(function (item) {
  return item % 2= = =0
})
console.log(find) / / 1
Copy the code