【问题标题】:remove matched elements from the array in jquery从jQuery中的数组中删除匹配的元素
【发布时间】:2014-05-19 09:32:03
【问题描述】:

我有一个类似的数据

userlist=[
    {
        "label": "veera_ss.com",
        "value": "veera_ss.com"
    },
    {
        "label": "v_v.com",
        "value": "v_v.com"
    },
    {
        "label": "sample_vh.com",
        "value": "sample_vh.com"
    },
    {
        "label": "sample_vh.com",
        "value": "sample_vh.com"
    },
    {
        "label": "nabeel.ahameed_vh.com",
        "value": "nabeel.ahameed_vh.com"
    }
]

我需要遍历这个并从这个列表中删除匹配的元素,例如如果值是v_v.com,sample_vh.com,它将从列表中删除。谢谢。

userlist=[
    {
        "label": "veera_ss.com",
        "value": "veera_ss.com"
    },
    {
        "label": "sample_vh.com",
        "value": "sample_vh.com"
    },
    {
        "label": "nabeel.ahameed_vh.com",
        "value": "nabeel.ahameed_vh.com"
    }
]

【问题讨论】:

  • 到目前为止你尝试过什么?我们能帮你。我们通常不会为您完成所有工作。

标签: javascript jquery arrays json arraylist


【解决方案1】:

您可以在这种情况下使用$.grep() 来实现您想要的..

试试,

var userlist =[{"label":"veera_ss.com","value":"veera_ss.com"},{"label":"v_v.com","value":"v_v.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}]

userlist = $.grep(userlist, function(val){
    return val.label !== 'v_v.com' && val.label !== 'sample_vh.com'
}).get(0);

【讨论】:

    【解决方案2】:

    您不需要 jquery。 Javascript 数组有一个方法:过滤器。你可以像这样使用它:

    [{"label":"veera_ss.com","value":"veera_ss.com"},
     {"label":"v_v.com","value":"v_v.com"},
     {"label":"sample_vh.com","value":"sample_vh.com"},
     {"label":"sample_vh.com","value":"sample_vh.com"},
     {"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}]
    .filter(function (el) {
         return el.label !== 'v_v.com' && el.label !== 'sample_vh.com'
    });
    

    【讨论】:

      【解决方案3】:

      你可以试试这个方法-

      var arr = []; // will hold final array after removing elements
      $.each(userlist,function(v){
        if(v.value != "sample_vh.com" && v.value != "sample_vh.com")
           arr.push(v);
      });
      

      【讨论】:

        【解决方案4】:

        你可以像这样使用underscorejs reject

        var userlist=[{"label":"veera_ss.com","value":"veera_ss.com"},{"label":"v_v.com","value":"v_v.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}];
        
        userlist = _.reject(userlist, function(user) {
            return user.value=="veera_ss.com" || user.value=="v_v.com";
        });
        

        【讨论】:

          【解决方案5】:

          创建一个新列表并进行比较

              var userlist=[{"label":"veera_ss.com","value":"veera_ss.com"},
                {"label":"v_v.com","value":"v_v.com"},
                {"label":"sample_vh.com","value":"sample_vh.com"},
                {"label":"sample_vh.com","value":"sample_vh.com"},
                {"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}]
          
              var newList = []
          
          
          userlist.forEach(function(item, key) { 
                            if(!newList.some(function(x){ 
                               return JSON.stringify(x) === JSON.stringify(item) }))
                                {
                                 newList.push(item);
                                } 
                           })
          
            //newList is filtered result.
          

          【讨论】:

            猜你喜欢
            • 2021-01-28
            • 2014-10-14
            • 1970-01-01
            • 1970-01-01
            • 2011-06-03
            • 1970-01-01
            • 2019-01-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多