簡體   English   中英

從jquery中的數組中刪除匹配的元素

[英]remove matched elements from the array in jquery

我有一個像

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"
    }
]

您可以在此上下文中使用$.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);

為此,您不需要 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'
});

你可以試試這個方法——

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);
});

您可以像這樣使用underscorejs 拒絕

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";
});

創建一個新列表並進行比較

    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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM