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.

Community
  • 1
  • 1
  • 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`. – Felix Kling Dec 05 '13 at 03:04
  • is the variable fruits is array or Object? because if you want delete the variable you need some id or name to related. – Guilherme Soares Dec 05 '13 at 03:05
  • Example if fruits is object but the key is fruits = {'apple':{values}}. Okay you only need put delete fruits['apple'] – Guilherme Soares Dec 05 '13 at 03:06
  • Hello everyone. I updated the question. @GuilhermeSoares, unfortunately in my situation, I don't have a single key but unique combinations of keys. –  Dec 05 '13 at 03:09
  • @thekalaban please check my response i hope help you. – Guilherme Soares Dec 05 '13 at 04:04

2 Answers2

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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thank you. How will I make this dynamic? As in how do I pass a new variable that can replace `apple`? –  Dec 05 '13 at 03:11
  • 1
    Make a function that accepts an array and a reference object, and put the code inside that function. – Felix Kling Dec 05 '13 at 03:12
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
Guilherme Soares
  • 726
  • 3
  • 7
  • 19