一、数组合并
var arr1 = [1,2,3,4];
var arr2 = [4,5,6,7,8];
var arr = arr1.concat(arr2); //合并数组
console.log(arr); //将set集合转化为数组
// 1,2,3,4,4,5,6,7,8
var arrNew= new Set(arr); //通过set集合去重
// 1,2,3,4,5,6,7,8
二、数组对象合并
var json1=[
{id:1,name:“aaa”},
{id:2,name:“bbb”},
{id:3,name:“ccc”},
]
var json2=[
{id:1,name:“ccc”},
{id:2,name:“ddd”},
{id:4,name:“eee”},
]
var json = json1.concat(json2); //两个数组对象合并
console.log(json)
// {id:1,name:“aaa”},{id:2,name:“bbb”},{id:3,name:“ccc”},{id:3,name:“ccc”},{id:4,name:“ddd”},{id:5,name:“eee”},
var json = json1.concat(json2); //两个数组对象合并
function newJson(json) {
let newJson = []; //盛放去重后数据的新数组
for(item1 of json){ //循环json数组对象的内容
let flag = true; //建立标记,判断数据是否重复,true为不重复
for(item2 of newJson){ //循环新数组的内容
if(item1.id==item2.id){ //让json数组对象的内容与新数组的内容作比较,相同的话,改变标记为false
flag = false;
if(flag){ //判断是否重复
newJson.push(item1); //不重复的放入新数组。 新数组的内容会继续进行上边的循环。
return newJson;
console.log(newJson(json));
// {id:1,name:“aaa”},{id:2,name:“bbb”},{id:3,name:“ccc”},{id:4,name:“ddd”},{id:5,name:“eee”},
let aWithOverrides = { ...a, x: 1, y: 2 };
let aWithOverrides = { ...a, ...{ x: 1, y: 2 } };
let x = 1, y = 2, aWithOverrides = { ...a, x, y };
let aWithOverrides = Object.assign({}, a, { x: 1, y: 2 });
{ name: "tony", id: "1", age: "20" },
{ name: "jack", id: "2", age: "21" },
{ name: "tony", id: "3", age: "50" },
{ na...
javascript数组去重利用数组的Array.prototype.indexOf来为数组对象扩展一个快速去重的方法。直接上代码:if(!Array.prototype.unique){
Array.prototype.unique = function() {
var a = [];//定义一个临时数组
for (var i = 0; i < this.l
function ArrSet(Arr: any[], id: string): any[] {
let obj: Object = {}
const arrays = Arr.reduce((setArr, item) => {
obj[item[id]] ? '' : (obj[item[id]] = true && setArr.push(item))
return setArr
}, [])