简体   繁体   中英

Javascript how to find the position of an object in an array

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?

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

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

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.

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM