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

1. The for loop is most commonly used

const arr=[1.33.444.6.7];
for (let i=0; i<arr.length; i++){console.log(arr[i]);
}
Copy the code

2. forEach()

2.1 Three Parameters

function(currentValue,index,arr)

  • CurrentValue Specifies the current element
  • Index Indicates the index of the current element
  • Arr Optional array object of the current element

2.2 Traversing an ordinary array

let arr = ["aaa"."bh"."word"]; 
arr.forEach(function(item,index,arr){ 	
  console.log(item,index,arr);
})
Copy the code

2.3 Traversing an Array of object types

let arr = [{"province":"ln"."cityId":12}, {"province":"sh"."cityId":122}]; 
arr.forEach(function(item,index,arr){
			console.log(item.province,index,arr);
        })
Copy the code

2.4 Whether to change the original array

Const array = [1, 2, 3, 4];

array.forEach(ele= > {
   ele = ele * 3
})
console.log(array); / / [1, 2, 3, 4]
Copy the code

(2) Reference type -> Array of similar objects changes the original array

const objArr = [{
    name: 'wxw'.age: 22
}, {
    name: 'wxw2'.age: 33
}]
objArr.forEach(ele= > {
    if (ele.name === 'wxw2') {
        ele.age = 88}})console.log(objArr); // [{name: "wxw", age: 22},{name: "wxw2", age: 88}]
Copy the code

(3) Reference type -> Change the entire single loop item does not change

const changeItemArr = [{
    name: 'wxw'.age: 22
}, {
    name: 'wxw2'.age: 33
}]
changeItemArr.forEach(ele= > {
    if (ele.name === 'wxw2') {
        ele = {
            name: 'change'.age: 77}}})console.log(changeItemArr); // [{name: "wxw", age: 22},{name: "wxw2", age: 33}]
Copy the code

3. The map () method

A map is when an array is “mapped” to a new array. Is a callback function that is executed for each element in the array.

  • Map does not detect empty arrays
  • Map returns a new array and does not alter the original array

3.1 Three Parameters

function(currentValue,index,arr)

  • CurrentValue Specifies the current element
  • Index Indicates the index of the current element
  • Arr Optional array object of the current element

(1) Use parseInt in the callback function

let arr = [1.0.2.2.3]; 
let newArr = arr.map(function(item,index,arr){  
console.log(parseInt(item),arr)  // Prints the traversal values and the original array
return parseInt(item) }) 
console.log(arr)   // The original array does not change its value
console.log(newArr) // The new array is parseInt
Copy the code

(2) parseInt() is passed as a callback to currentValue,index, and arr

let arr = [1.0.2.2.3]; / / [parseInt (1, 0), parseInt (2.2, 1), parseInt (3, 2)))
let newArr = arr.map(parseInt)
console.log(arr)
console.log(newArr)
Copy the code

The second argument to parseInt is a numeric base, which is a number of bases

  • If the parameter is omitted or the value is 0, it is parsed in decimal notation
  • Press hexadecimal starting with “0x” or “0x”
  • If this parameter is less than 2 or greater than 36, parseInt() will return NaN

For in is the ES5 standard. This method has low efficiency in iterating through the number group and is mainly used to loop through the attribute 1 of the object. Iterating through a group of numbers yields the index

let arr = [1.0.2.2.3];
for(let i in arr){            / / I is the index
console.log(arr[i])
}
Copy the code

3.2 Traversing an object yields a key

let arr ={"aaa":12."bbb":233};
for(let i in arr){
console.log(i);     // aaa bbb
console.log(arr[i]);   / / 12, 233
}
Copy the code

4. For in

For in is an ES5 standard. This method is inefficient in iterating over a number group and is mainly used to iterate over an object’s attributes

4.1 The index is obtained by traversing the number group

let arr = [1.0.2.2.3];
for(let i in arr){            / / I is the index
console.log(arr[i])
}
Copy the code

4.2 Traversing an object obtains a key

let arr ={"aaa":12."bbb":233};
for(let i in arr){
console.log(i);     // aaa bbb
console.log(arr[i]);   / / 12, 233
}
Copy the code

5. For example

Was new to es6

5.1 Use arrays directly

let arr = [1.8.9.10];
for(let value of arr ){
    console.log(value);
}
Copy the code

5.2 This is used for ordinary objects

Object.keys()let arr ={"aaa":12."bbb":233};
for(let key of Object.keys(arr)){
console.log(arr[key]);
}
Copy the code

Object.values()

et arr ={"pp":123."dd":99};
console.log(Object.values(arr));        / / 123 [13]
Copy the code