如何在不添加重复项的情况下使用jquery连接两个数组对象 [英] How to concatenate two array objects using jquery without adding duplicates

查看:82
本文介绍了如何在不添加重复项的情况下使用jquery连接两个数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组对象:

 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:

I do jquery like follows:

$.each(arr1 , function(){ 
      var productId = this.product_id;
       $.each(arr2 , function(productId){
        if(this.product_id != productId){
           arr2.push(this);
        }
       }); 
   });

最后

arr2必须看起来像

arr2 must look like

     var  arr2 = [{product_id: 2, name: 'stack'}, {product_id: 3, name: 'overflow'},
             {product_id: 6, name: 'foo'}]

我在做正确的jquery编码吗??

am i doing correct jquery coding..?

推荐答案

$.extend(arr1,arr2)

这将从arr2复制(并覆盖重复项)到arr1.

This will copy (and overwrite duplicates) from arr2 to arr1.

参考: http://api.jquery.com/jQuery.extend/

这篇关于如何在不添加重复项的情况下使用jquery连接两个数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆