es6 语法 (map、set和array 的对比)

时间:2021-10-28 21:04:38
//数据结构对比 增查改删

{
//map和array对比 let map = new Map();
let array = []; //增
map.set('t',1);
array.push({t:1});
console.info('map-array',map,array) //{"t"=>1};-[0:{t:1}] //查
let map_exist = map.has('t');
let array_exist = array.find(item=>item.t);//true
console.info('exist',map_exist,array_exist);//{t:1} //改
map.set('t',2);
array.forEach(item=>item.t?2:'');
console.info('map-array-modify',map,array);//{"t"=>2};0:{t: 1} //删
map.delete('t');
let index = array.findIndex(item=>item.t);
array.splice(index,1);
console.info('map-array-empty',map,array);//{};[]
} { //set和array对比
let set = new Set();
let array = []; //增
set.add({'t':1});
array.push({t:1});
console.info('set-array',set,array);//0:value{t:1};0:{t: 1} //查
let set_exist = set.has({t:1});
let array_exist = array.find(item=>item.t);
console.info('exist',set_exist,array_exist);//false;{t:1}; //改
set.forEach(item=>item.t?item.t=2:'');
array.forEach(item=>item.t?item.t=2:'');
console.info(set,array);//0:value{t:2};0:{t: 2} //删
set.forEach(item=>item.t?set.delete(item):'');
let index = array.findIndex(item=>item.t);
array.splice(index,1);
console.info('set-array-empty',set,array);//{};[] }