我有两个数组对象:
var arr1 =[{product_id: 2, name: 'stack'}, {product_id: 3, name: 'overflow'}];
var arr2 = [{product_id: 2, name: 'popo'},{product_id: 6, name: 'foo'}];
我做了如下的jquery:
$.each(arr1 , function(){
var productId = this.product_id;
$.each(arr2 , function(productId){
if(this.product_id != productId){
arr2.push(this);
}
});
});
最后
arr2必须看起来像
var arr2 = [{product_id: 2, name: 'stack'}, {product_id: 3, name: 'overflow'},
{product_id: 6, name: 'foo'}]
我正在做正确的jquery编码..?
答案 0 :(得分:5)
答案 1 :(得分:4)
$.extend(true, arr1, arr2);
Extend将两个对象/数组连接到第一个对象。