Question

In JavaScript, how can an object or row be deleted from another object that contains it given the first?

var apple = { 'color': 'red', 'price': 100, 'origin': 'Japan' };
var fruits = { Object, Object, Object }; // objects: apple, orange, mango
delete fruits[apple]; // this does not work

The 'delete fruits[apple]' or it's other forms as explained in this SO thread are not working.

Is removing an 'object1' from an 'object2' possible by just providing 'object1' as a parameter as indicated before?

The method indicated in Deleting a row from javascript object:

var newFruits = fruits.filter(function( apple ) {
  return fruits.color != apple.color &&
      fruits.price != apple.price && 
      fruits.origin != apple.origin;
});

Does not work either.

Edit

The fruits just contains rows of { 'color', 'price' , 'origin' } hence the last method. I actually need to compare these three components from a new array that might be inside the fruits array.

Was it helpful?

Solution

The correct application of the method from the other question would be:

var apple = { 'color': 'red', 'price': 100, 'origin': 'Japan' };
var newFruits = fruits.filter(function( obj ) {
  return obj.color != apple.color &&
      obj.price != apple.price && 
      obj.origin != apple.origin;
});

You tried to access fruits.color, which always returns undefined since arrays don't have a color property. Instead, you have to compare the properties of each element of the array against your reference object. I suggest to have a look at the MDN documentation to learn how .filter works (which arguments it expects, which arguments get passed to the callback, etc).

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top