简体   繁体   English

Javascript如何在数组中找到对象的位置

[英]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. 我一直在试图获取具有特定x和y坐标的对象在数组中的位置。

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. 当我尝试调用该函数时,它似乎将数组读取为[object,object,object],并返回-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. 问题是obj是与列表中的对象不同的对象。 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. 您将需要用一个循环将indexOf替换,该循环一次检查每个元素。 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. 实际上,这就是indexOf函数中发生的事情,只是indexOf进行了严格的三等式(===)相等检查,除非对象完全相同,否则它不适用于一个对象。

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

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

indexOf with object won't work for you. 带对象的indexOf不适用于您。

I won't loop through all the array to find the object because it will be in complexity of O(n). 我不会遍历所有数组来查找对象,因为它的复杂度为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: 我建议创建一个键为x_y并将值作为对象本身的映射,类似的东西应该对您有用:

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. 那只是我的2美分。

Good luck anyways. 无论如何,祝你好运。

As Mozilla describes it: 正如Mozilla 所描述的那样

indexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator). indexOf使用严格相等(===或三重相等运算符使用的相同方法)将searchElement与Array的元素进行比较。

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 您可以遍历每个对象并使用splice方法将其删除

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 jsfiddle检查您的浏览器控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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