2 may be useful

1.map()

Original Address --- https://blog.csdn.net/liminwang0311/article/details/86480829

map () method creates a new array, the result is returned after the results of each element in the array to call a function provided.

var a=['a','s','d','f','g'];
a.map(function(item,index){
//a,s,d,f,g
console.log(item);
//0,1,2,3,4
console.log(index)
})

map () method creates a new array, but not in traversed new array array1 before being assigned, but once you get a value for each traversal.

var array1 = [1, 4, 9, 16];
const map1 = array1.map(x => {
    if (x == 4) {
        return x * 2;
    }
});
console.log(map1);
打印结果为:
> Array [undefined, 8, undefined, undefined]

Why is there three undefined it? And I did not expect [1,8,9,16].

Write just added a condition, that is, when the value of x 4 multiplied by 2, the reason why there will be undefined, because the map () method creates a new array, but not in the new array before being traversed array1 assignment, but once you get a value for each traversal. So, following such an amendment after correctly:

var array1 = [1, 4, 9, 16];
const map1 = array1.map(x => {
    if (x == 4) {
        return x * 2;
    }
    return x;
});

 

Guess you like

Origin www.cnblogs.com/ybx-cs/p/10956626.html
May