1、将字符串数组转换为数字数组。
const numbers = ["1", "2", "3"].map(Number); // [1, 2, 3]
2、对数组中的每个元素进行数学运算。
const squares = [1, 2, 3].map(x => x * x); // [1, 4, 9]
3、从对象数组中提取特定属性形成新数组。
const people = [{name: 'Alice'}, {name: 'Bob'}];
const names = people.map(person => person.name); // ['Alice', 'Bob']
4、利用箭头函数和解构等ES6特性,使代码更加简洁。
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
const ids = users.map(({id}) => id); // [1, 2]
5、如果你有一个对象数组,可以用来快速重命名或调整对象的结构。
const users = [
{firstName: "John", lastName: "Doe"},
{firstName: "Jane", lastName: "Doe"}
];
const renamedUsers = users.map(user => ({name: user.firstName + " " + user.lastName}));
// [{name: "John Doe"}, {name: "Jane Doe"}]