rimuovere l’elemento dalla matrice usando il suo nome / valore

Ho il seguente array

var countries = {}; countries.results = [ {id:'AF',name:'Afghanistan'}, {id:'AL',name:'Albania'}, {id:'DZ',name:'Algeria'} ]; 

Come posso rimuovere un object da questo array usando il suo nome o id?

Grazie

 Array.prototype.removeValue = function(name, value){ var array = $.map(this, function(v,i){ return v[name] === value ? null : v; }); this.length = 0; //clear original array this.push.apply(this, array); //push all elements except the one we want to delete } countries.results.removeValue('name', 'Albania'); 

Creata una comoda funzione per questo ..

 function findAndRemove(array, property, value) { array.forEach(function(result, index) { if(result[property] === value) { //Remove from array array.splice(index, 1); } }); } //Checks countries.result for an object with a property of 'id' whose value is 'AF' //Then removes it ;p findAndRemove(countries.results, 'id', 'AF'); 

Prova questo:

 var COUNTRY_ID = 'AL'; countries.results = countries.results.filter(function(el){ return el.id != COUNTRY_ID; }); 

Prova questo. (IE8 +)

 //Define function function removeJsonAttrs(json,attrs){ return JSON.parse(JSON.stringify(json,function(k,v){ return attrs.indexOf(k)!==-1 ? undefined: v; }));} //use object var countries = {}; countries.results = [ {id:'AF',name:'Afghanistan'}, {id:'AL',name:'Albania'}, {id:'DZ',name:'Algeria'} ]; countries = removeJsonAttrs(countries,["name"]); //use array var arr = [ {id:'AF',name:'Afghanistan'}, {id:'AL',name:'Albania'}, {id:'DZ',name:'Algeria'} ]; arr = removeJsonAttrs(arr,["name"]); 

Puoi eliminare da 1 o più proprietà:

 //Delets an json object from array by given object properties. //Exp. someJasonCollection.deleteWhereMatches({ l: 1039, v: '3' }); -> //removes all items with property l=1039 and property v='3'. Array.prototype.deleteWhereMatches = function (matchObj) { var indexes = this.findIndexes(matchObj).sort(function (a, b) { return b > a; }); var deleted = 0; for (var i = 0, count = indexes.length; i < count; i++) { this.splice(indexes[i], 1); deleted++; } return deleted; } 

puoi usare l’operatore delete per cancellare la proprietà con il suo nome

 delete objectExpression.property 

o scorrere l’object e trovare il valore che ti serve ed eliminarlo:

 for(prop in Obj){ if(Obj.hasOwnProperty(prop)){ if(Obj[prop] === 'myValue'){ delete Obj[prop]; } } } 

Questo richiede solo javascript e appare un po ‘più leggibile rispetto ad altre risposte. (Suppongo che quando scrivi ‘valore’ intendi ‘id’)

 //your code var countries = {}; countries.results = [ {id:'AF',name:'Afghanistan'}, {id:'AL',name:'Albania'}, {id:'DZ',name:'Algeria'} ]; // solution: //function to remove a value from the json array function removeItem(obj, prop, val) { var c, found=false; for(c in obj) { if(obj[c][prop] == val) { found=true; break; } } if(found){ delete obj[c]; } } //example: call the 'remove' function to remove an item by id. removeItem(countries.results,'id','AF'); //example2: call the 'remove' function to remove an item by name. removeItem(countries.results,'name','Albania'); // print our result to console to check it works ! for(c in countries.results) { console.log(countries.results[c].id); } 

ha funzionato per me ..

 countries.results= $.grep(countries.results, function (e) { if(e.id!= currentID) { return true; } });