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)
<script>function extend(des, src, override){
if(src instanceof Array){
for(var i = 0, len = src.length i < len i++)
extend(des, src[i], override)
}
for( var i in src){
if(override || !(i in des)){
des[i] = src[i]
}
}
return des
}
var a ={"a":"1","b":"2"}
var b ={"c":"3","d":"4","e":"5"}
var c = extend({}, [a,b])
</script>
这个算是比较好理解的了。
思路 转换成字符串再转化成json;
代码如下:
var json = {}var json1 = {a:1,b:1}
var json2 = {c:1,d:1}
json = eval('('+(JSON.stringify(json1)+JSON.stringify(json2)).replace(/}{/,',')+')')
// json: {a:1,b:1,c:1,d:1}