0

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.

5
  • Is fruits an array? FYI, you implemented the method presented in the other question incorrectly. If fruits is your array then it won't have a property color. Commented Dec 5, 2013 at 3:04
  • is the variable fruits is array or Object? because if you want delete the variable you need some id or name to related. Commented Dec 5, 2013 at 3:05
  • Example if fruits is object but the key is fruits = {'apple':{values}}. Okay you only need put delete fruits['apple'] Commented Dec 5, 2013 at 3:06
  • Hello everyone. I updated the question. @GuilhermeSoares, unfortunately in my situation, I don't have a single key but unique combinations of keys. Commented Dec 5, 2013 at 3:09
  • @thekalaban please check my response i hope help you. Commented Dec 5, 2013 at 4:04

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. How will I make this dynamic? As in how do I pass a new variable that can replace apple?
Make a function that accepts an array and a reference object, and put the code inside that function.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.