合并json用到对象方法:Object.assign(),注意此方法是如有相同键值时后面覆盖前面,实例代码如下:
const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 }; const returnedTarget = Object.assign(target, source); console.log(target); // expected output: Object { a: 1, b: 4, c: 5 } console.log(returnedTarget === target); // expected output: true
合并成数组用到:arr.push(),实例代码如下:
var obj1 = { a: 1, b: 2 }; var obj2 = { a: 11, b: 22}; var arr=[]; arr.push(obj1); //arr.push(obj2); //输出结果: [{ a: 1, b: 2},{ a: 11, b: 22}]
删除json用到:delete,实例代码如下:
var obj = {‘id’:1, ‘name’:2}; delete obj.id; 或 delete obj[id]; console.log(obj); // {‘name’:2}
讨论数量:0