Map
是es6新增的一种方法,下面web建站小编就利用Map
现在2个数组的合并及克隆!
let arr1 = [{name:'es6', year:'2022-10-10'},{name:'web', year:'2022-10-11'}] let arr2 = [{name:'my', year:'2022-10-12'},{name:'php', year:'2022-10-15'}] let arrMap1 = new Map([arr1]); let arrMap2 = new Map([arr2]); let merge = new Map([...arrMap1, ...arrMap2]); console.log('输出结果:', merge) 输出结果: Map(2) {{…} => {…}, {…} => {…}} [[Entries]] 0: {Object => Object} key: {name: 'es6', year: '2022-10-10'} value: {name: 'web', year: '2022-10-11'} 1: {Object => Object} key: {name: 'my', year: '2022-10-12'} value: {name: 'php', year: '2022-10-15'} size: 2
克隆数组
let arr1 = [{name:'es6', year:'2022-10-10'},{name:'web', year:'2022-10-11'}] let arr2 = new Map([arr1]); console.log(arr2); //Map(1) {{…} => {…}} //[[Entries]] //0: {Object => Object} //key: {name: 'es6', year: '2022-10-10'} //value: {name: 'web', year: '2022-10-11'} //size: 1
js数组内根据相同key合并,里面的数组也合并(兼容ie9/ie10浏览器)
上面是“es6利用Map实现数组合并(克隆)”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_2603.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!