I have object with bad indexes. I can't edit this, because i application this working ok.
var object = { "aaa": 1, "aaa": 2, "aaa" : 3, "aaa" : 4};
$.each(object, function(key, value){
});
how can i remove all values where value = 2 and 4?
I have object with bad indexes. I can't edit this, because i application this working ok.
var object = { "aaa": 1, "aaa": 2, "aaa" : 3, "aaa" : 4};
$.each(object, function(key, value){
});
how can i remove all values where value = 2 and 4?
You can't. Every value overwrites the previous one in the literal.
$> ({ "aaa": 1, "aaa": 2, "aaa" : 3, "aaa" : 4});
Object
aaa : 4
$>
You will have to ensure that the object literal is well-formed, then use something like this (ES5) :
var filteredObject = Object.keys( yourObject ).reduce( function ( target, key ) {
if ( yourObject[ key ] !== 2 && yourObject[ key ] !== 4 )
target[ key ] = yourObject[ key ];
return target;
}, { } );
Can't you simply do an if(value!=2 && value!=4) into the for?
You can try the delete
keyword to remove the key and that will remove the value
Here is the code
var object = { "a": 1, "b": 2, "c" : 3, "d" : 4};
$.each(object, function(key, value){
if(value==2 || value==4){
delete object[key];
}
});
Note : I assumed that the same keys in your example is an error.
To remove all values where value == 4
:
for (var member in object) { if (object[member]==4) {delete object[member]} }