简体   繁体   中英

remove matched elements from the array in jquery

I having a data like

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

i need to iterate through this and remove the matched elements from this list for example if the value is v_v.com,sample_vh.com that will remove from the list. thanks.

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

You can use $.grep() in this context to achieve what you want..

Try,

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

You do not need jquery for this. Javascript array has a method: filter. You can use it like:

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

You can try this way -

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

You could use underscorejs reject like this:

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

create a new list and compare

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM