The elements in the array are converted into Number or String---array map method

The elements in the array are converted to Number type

let num: any = ["1", "2", "3"];
num = num.map(Number);
console.log(num); //[1, 2, 3]

The elements in the array are converted to String type

let str: any = [1, 2, 3];
str = str.map(String);
console.log(str); //["1", "2", "3"]

Extended map method

definition:

const mapObj = [[1, 2],[3, 4]];

const newMap = mapObj.map(([x, y]) => x + y);
//[ 3, 7 ] map()返回的是一个新数组,数组中的元素为原始数据元素调用函数处理后的值
//且map()按照原始数组元素顺序依次处理元素
console.log(newMap); 
console.log(mapObj); //map()不会改变原始数据
console.log([].map((item) => item));//[],map()不会对空数组进行检测

grammar:

array.map(function(currentValue,index,arr), thisValue)

parameter:

  1. function(currentValue,index,arr)

required. Function, each element in the array will execute this function Function
parameters:
currentValue: Required. The value index of the current element
: optional. The index value of the current element
arr: optional. the array object the current element belongs to

  1. thisValue

optional. The object to be used as the execution callback, passed to the function, and used as the value of "this".
If thisValue is omitted, or null or undefined is passed in, then this of the callback function is the global object.

Guess you like

Origin blog.csdn.net/lfwoman/article/details/119907723