How resolve this bugs:
The frist you need some key to get values:
Model
Example if you working if fruits:
var fruits =
{
"apple": {
'color': 'red',
'price': 100,
'origin': 'Japan'
}
//other fruits
}
How delete some fruit from variable fruits
it's easy only need this code below:
delete fruits['apple'];
Method filter
I didn't understand why did you use it to try delete.
var newFruits = fruits.filter(function( apple ) {
return fruits.color != apple.color &&
fruits.price != apple.price &&
fruits.origin != apple.origin;
});
Refactory I create class to manipulate fruits
var Fruits = function(){
this.values = {
"apple": {
'color': 'red',
'price': 100,
'origin': 'Japan'
}
}
this.filter = function(obj){
var f = {}; //list of fruits
//Loop in all fruits
for(var name in this.values){
var fruit = this.values[name];
var validFruit = true;
//Check the obj values..
for(prop_name in obj){
var prop_value = obj[prop_name];
if((typeof fruit[prop_name] === "undefined") || (fruit[prop_name] !== prop_value)){
validFruit = false; //Is not valid
}
}
if(validFruit === true){
f[name] = fruit;
}
}
return f;
};
this.deleteIfHas = function(obj){
var list = this.filter(obj);
for(var key in list){
delete this.values[key];
}
}
this.setFruit = function(name, info){
this.values[name] = info;
}
this.getFruit = function(name){
return (typeof this.values[name] !== "undefined") ? this.values[name] : false;
}
}
Testing..
var classFruit = new Fruits();
console.log(classFruit.filter({"origin":"Japan"})); // {"apple":{values of apple}}
classFruit.deleteIfHas({"origin":"Japan"}); //Delete apple
console.log(classFruit.values); //return {} because i delete apple