ES5 common array of common methods

  • ES3 array of methods are common methods before we have said
  • Today it is in some ways ES5

indexOf

    • indexOf to find a particular index of the array
    • Syntax: indexOf (item you're looking for in the array)

 

was arr = [1, 2, 3, 4, 5 ]

// Use indexOf super find a particular array 
var index = arr.indexOf (. 3 )

console.log(index) // 2
  • We're looking for the array that a value of 3
  • Returns the value of 3 is that an index in the array

 

    • If you're looking for is not in the array, it will return -1

 

was arr = [1, 2, 3, 4, 5 ]

// Use indexOf super find a particular array 
var index = arr.indexOf (10 )

console.log(index) // -1
  • You're looking for value does not exist in the array, it will return -1

 

forEach

    • And the role of a for loop is used to iterate
    • 语法:arr.forEach(function (item, index, arr) {})

 

was arr = [1, 2, 3 ]

// Use forEach traverse the array 
arr.forEach ( function (Item, index, ARR) {
   // Item Each array is a 
  // index is the index into the array 
  // ARR is the original array 
  console.log ( 'first array '+ index +' is the value of the item '+ item +', the original array is' , ARR)
})
  • That function forEach () when transmitted, it will be based on the length of the array
  • What is the length of the array, this function executes how many times

 

map

    • And forEach similar, but it can operate on each item in the array and returns a new array

 

was arr = [1, 2, 3 ]

// use the map to traverse the array 
var newArr = arr.map ( function (Item, index, ARR) {
   // Item is, each entry in the array 
  // index is the index into the array 
  // ARR original array is the 
  return Item 10 +
})

console.log(newArr) // [11, 12, 13]

filter

    • And use map similar to our terms to filter array
    • The original array to meet the conditions screened to form a new array is returned

 

was arr = [1, 2, 3 ]

// use the filter array filters 
var newArr = arr.filter ( function (Item, index, ARR) {
   // Item Each array is a 
  // index is the index into the array 
  // ARR original array is the 
  return Item>. 1
})

console.log(newArr) // [2, 3]
  • The conditions we set is> 1
  • The new array will return all> Item 1 of the original array

Guess you like

Origin www.cnblogs.com/Kuoblog/p/12442840.html