js如何处理两个json数组去除重复数据,然后合并数组,求大神帮忙解决下
例如下面两个json数组,a和b,按照a中的id字段对b进行去重,然后合并到一个json数组,排序方式:数组a中数据保持原顺序,把b数据按照去重后的顺序排在a数据后边。v...
例如下面两个json数组,a和b,按照a中的id字段对b进行去重,然后合并到一个json数组,排序方式:数组a中数据保持原顺序,把b数据按照去重后的顺序排在a数据后边。
var a = [
{
"id":"1001",
"name":"张三",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1002",
"name":"李四",
"age":"15",
"address":"北京市海淀区",
"school":"海淀区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
}
];
var b = [
{
"id":"1004",
"name":"小毛",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
}
]
去重后结果:
var c = [
{
"id":"1001",
"name":"张三",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1002",
"name":"李四",
"age":"15",
"address":"北京市海淀区",
"school":"海淀区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
},
{
"id":"1004",
"name":"小毛",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
}
];
跪求大神帮忙用js解决一下!!! 展开
var a = [
{
"id":"1001",
"name":"张三",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1002",
"name":"李四",
"age":"15",
"address":"北京市海淀区",
"school":"海淀区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
}
];
var b = [
{
"id":"1004",
"name":"小毛",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
}
]
去重后结果:
var c = [
{
"id":"1001",
"name":"张三",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1002",
"name":"李四",
"age":"15",
"address":"北京市海淀区",
"school":"海淀区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
},
{
"id":"1004",
"name":"小毛",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
}
];
跪求大神帮忙用js解决一下!!! 展开
2个回答
2017-09-15
展开全部
var c = a.concat(b),//合并成一个数组
temp = {},//用于id判断重复
result = [];//最后的新数组
//遍历c数组,将每个item.id在temp中是否存在值做判断,如不存在则对应的item赋值给新数组,并将temp中item.id对应的key赋值,下次对相同值做判断时便不会走此分支,达到判断重复值的目的;
c.map((item,index)=>{
if(!temp[item.id]){
result.push(item);
temp[item.id] = true
}
})
console.log(result)
追答
箭头函数兼容性问题,改成
c.map(function(item,index){
if(!temp[item.id]){
result.push(item);
temp[item.id] = true
}
})
展开全部
<script>
var a = [1,2,3,4,5];
var b = [1,2,4,5];
alert(tab(a,b));
function tab(arr1,arr2){
var arr = arr1.concat(arr2);
var lastArr = [];
for(var i = 0;i<arr.length;i++)
{
if(! unique(arr[i],lastArr))
{
lastArr.push(arr[i]);
}
}
return lastArr;
}
function unique(n,arr)
{
for(var i=0;i<arr.length;i++)
{
if(n==arr[i]){
return true;
}
}
return false;
}
</script>
var a = [1,2,3,4,5];
var b = [1,2,4,5];
alert(tab(a,b));
function tab(arr1,arr2){
var arr = arr1.concat(arr2);
var lastArr = [];
for(var i = 0;i<arr.length;i++)
{
if(! unique(arr[i],lastArr))
{
lastArr.push(arr[i]);
}
}
return lastArr;
}
function unique(n,arr)
{
for(var i=0;i<arr.length;i++)
{
if(n==arr[i]){
return true;
}
}
return false;
}
</script>
追问
大神,按照我给出的json数据,多字段的,不是单一的数组去重
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询