let ha = [
{id:'H',name:'3'},
{id:'A',name:'6'},
{id:'B',name:'14'},
{id:'C',name:'60'}
];
let hb= [
{id:'H',name:'2'},
{id:'A',name:'6'},
{id:'B',name:'16'},
{id:'N',name:'2'},
{id:'C',name:'19'}
];
// 合并两个json数组,并去重;
ha= Object.assign([], ha, hb);
console.log('ha',ha);
// [
{id:'H',name:'2'},
{id:'A',name:'6'},
{id:'B',name:'16'},
{id:'N',name:'2'},
{id:'C',name:'19'}
];
// 去除json数组中的某一项
ha.splice(3, 1);
console.log('ha',ha);
// [
{id:'H',name:'2'},
{id:'A',name:'6'},
{id:'B',name:'16'},
{id:'C',name:'19'}
];
一、Object.assign()方法,浅拷贝、对象属性的合并(数组是对象)
二、splice()方法
在W3C中我们可以查到有关于splice()的描述:
arrayObject.splice(index, howmany, item1, ....., itemX) 方法向/从数组中添加/删除项目,然后返回被删除的项目。
参数1:index 为插入添加或(和)删除的起始位置;
参数2:howmany 指定从数组中添加/删除的元素个数;
参数3:item1, ....., itemX 可选,选择添加操作时填入,表示需要添加的元素。
参考:JS中彻底删除json对象组成的数组中的元素
js删除json中指定的元素
JS删除数组里的某个元素方法