1、删除指定索引
复制代码function Arrdel(a1, a2) {
let arr = [];
a2.forEach(item => {
a1.splice(item, 1)
})
//过滤
arr = a1.filter(item => item);
return arr;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2、拿到相同数组和不同数组
复制代码let arr1 = [{examNo:1231,name:'中国',age:20},{examNo:3211,name:'美国',age:20}]
let arr2 = [{examNo:1231,sex:'男',fam:2},{examNo:'',sex:'女',fam:21}]
let appointmentListArr1 = [];
let appointmentListArr2 = [];
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i].examNo === arr2[j].examNo) {
//获取相同数组
appointmentListArr1.push(arr1[i]);
appointmentListArr2.push(arr2[j]);
//删除相同数组
this.Arrdel(arr1, [i])
this.Arrdel(arr2, [j])
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
3、相同数组合并
复制代码const appointmentListArr = appointmentListArr1.map((item, index) => {
return { ...item, ...appointmentListArr2[index] };
});
- 1
- 2
- 3
4、不同数组合并
复制代码const appointmentListArr3 = arr1.map((item, index) => {
return { ...item, ...arr2[index] };
});
- 1
- 2
- 3
5、总合并
复制代码const appointmentListArrList = appointmentListArr.concat(appointmentListArr3)
- 1
6、结果
复制代码console.log('结果',appointmentListArrList)
[
{
"examNo": 1231,
"name": "中国",
"age": 20,
"sex": "男",
"fam": 2
},
{
"examNo": "",
"name": "美国",
"age": 20,
"sex": "女",
"fam": 21
}
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
js根据相同key合并生成子数组children(兼容ie10)
javascript根据相同id合并形成child子数组(支持低版本浏览器)
标签: 合并
上面是“2个数组,相同数据合并后不同数据合并(删除指定”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_121.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!