js map()方法和Map对象的使用

map方法

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

map() 方法按照原始数组元素顺序依次处理元素。

let a = [1,2,3,4,5];
let b = ['a','b','c','d','e'];
let temp = a.map((item,index,arr)=>({
        name:arr[index],
        value:item
}))//[{"name": 1,"value": 1},{"name": 2,"value": 2},{"name": 3,"value": 3},{"name": 4,"value": 4},{"name": 5,"value": 5}]

其中,item是元素,index是索引,arr是a数组。

Map对象

 

Map 对象存有键值对,其中的键可以是任何数据类型。

 

Map 对象记得键的原始插入顺序。

 

Map 对象具有表示映射大小的属性。

创建Map

let map = new Map();
map.set("key",1);

获取值

map.get("key"); // 1

clear() 删除 Map 中的所有元素。

delete() 删除由键指定的元素。

has() 如果键存在,则返回 true。

forEach() 为每个键/值对调用回调。

 

posted @ 2022-06-30 15:55  Finn--  阅读(588)  评论(0)    收藏  举报