我有两个数组对象:
var arr1 =[{product_id: 2, name: 'stack'}, {product_id: 3, name: 'overflow'}];
var arr2 = [{product_id: 2, name: 'popo'},{product_id: 6, name: 'foo'}];
Run Code Online (Sandbox Code Playgroud)
我做jquery如下:
$.each(arr1 , function(){
var productId = this.product_id;
$.each(arr2 , function(productId){
if(this.product_id != productId){
arr2.push(this);
}
});
});
Run Code Online (Sandbox Code Playgroud)
在末尾
arr2必须看起来像
var arr2 = [{product_id: 2, name: 'stack'}, {product_id: 3, name: 'overflow'},
{product_id: 6, name: 'foo'}]
Run Code Online (Sandbox Code Playgroud)
我在做正确的jquery编码..?