0

This is my first question here, so pardon me if I get something wrong.

I have an array of coordinates organized as objects, and I need to find and delete a certain object. I am stuck trying to get the position of the object with the specific x and y coordinates in the array.

Here is where I got to:

door_array = [{x:3, y:4},{x: 12,y: 12}, {x: 15,y:15}];

function remove_door(dx,dy, array) 
{
    var obj = {x:dx,y:dy};  
    var a = door_array.indexOf(obj); //this part doesn't work
    door_array.slice(a,1)   

}

When i try to call the function, it appears to read the array as [object,object,object], and returns -1.

The question is, how do I find the position of the specific object via it's coordinates so I can delete it?

3
  • 1
    the problem is that you are creating a new object (with the same values as the one you are looking for). This is not the same object though. You would probably need to filter by values or ad an id... Commented Nov 11, 2014 at 18:04
  • {x: 4} === {x: 4} // false Commented Nov 11, 2014 at 18:04
  • using indexOf( object ) compares by reference (or by pointers) that's why it's failing. Commented Nov 11, 2014 at 18:07

5 Answers 5

2

The problem is obj is a different object than the one in the list. You should loop through the objects in the list till you find the one you need. Ex.

door_array = [{x:3, y:4},{x: 12,y: 12}, {x: 15,y:15}];

function remove_door(dx,dy, array) 
{
    var index = -1;
    for(var i = 0; i < array.length; i++)
    {
        if(array[i].x == dx && array[i].y == dy)
        {
            index = i;
            break;
        }
    }
    if(index != -1)
    {
        array.slice(index,1);
    } 
    return array;
}

You should return array after you are done manipulating it. Call like this:

door_array = remove_door(x, y, door_array);
Sign up to request clarification or add additional context in comments.

4 Comments

the if body should operate on array that is passed as an argument, not on door_array
and you should return array since slice does not operate in place
Thank you very much for that(all who took the time to answer). it now returns the correct value(index), but for some reason it didn't delete the object from the array. I changed array.slice to array.splice, and now it works like it should, though I am not sure why slice didn't do it.
Glad you got it figured out!
0

You can iterate through each object and delete them with splice method

door_array = [{x:3, y:4},{x: 12,y: 12}, {x: 15,y:15}];

function remove_door(dx,dy, arrays) 
{
    var obj = {x:dx,y:dy};
    for(i=0; i<arrays.length; i++){

    var a = arrays[i]; 

        if(a.x == obj.x && a.y == obj.y){
        console.log('found');
            arrays.splice(i,1);
        }

        }
    console.log(arrays);
    return arrays;


}
remove_door(12,12,door_array);

Jsfiddle check your browser console

2 Comments

Thank you, I already got it to work from a previous answer, but will keep this method in mind.
Okay no problem. Check this. if you have any doubt, just ask me:)
0

You will need to replace indexOf with a loop that checks each element one at a time. That is effectively what is happening in the indexOf function, except that indexOf is doing a strict, triple-equals (===) equality check, which won't work for an object unless it is the exact same one.

var x = {};
var y = {};

y === x; // false
x === x; // true

Comments

0

indexOf with object won't work for you.

I won't loop through all the array to find the object because it will be in complexity of O(n).

I would suggest to create a map with key as x_y and value as the object itself, something like that should work for you:

var map = {};
map["3_4"] = {x:3, y:4};
map["2_12"] = {x:2, y:12};

// then you can get the value with O(1)
var requiredValue = map[dx + "_" + dy];

That's just my 2 cents.

Good luck anyways.

Comments

0

As Mozilla describes it:

indexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).

And triple equals doesn't do deep object comparison, it compares by reference. You can re-factor your code to this:

door_array = [{x:3, y:4},{x: 12,y: 12}, {x: 15,y:15}];

function remove_door(dx,dy, array) 
{
    var obj = {x:dx,y:dy};
    for (var i = 0; i < door_array.length; i++){
       if(door_array[i].x === obj.x && door_array[i].y === obj.y)
          return i;
    }
    return -1;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.