<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>map方法</title>
</head>
<body>
<script>
/*
* 场景1
*
* 假定有一个数组(A),将A数组中的值以双倍的形式放到B数组
*/
let numbers = [1,2,3];
var doubledNumbers = numbers.map(number =>{
return number * 2;
});
console.log(doubledNumbers);
// 遍历打印
doubledNumbers.forEach(item =>{
console.log(item);
});
/*
* 场景2
*
* 假定有一个对象数组(A),将A数组中对象某个属性的值存储到B数组中
*/
let cars = [
{model:"Buick",price:"CHEAP"},
{model:"BMW",price:"expensive"}
];
let prices = cars.map(car =>{
return car.price;
});
console.log(prices);
/* map需要返回值,如果不给return,默认返回undefined,map返回的是一个新数组 */
</script>
</body>
</html>
es6 map方法
最新推荐文章于 2022-10-26 16:00:33 发布