简体   繁体   English

从数组中的对象中查找键的值-Javascript

[英]Finding a Key's Value from an object within an array - Javascript

I have tried many things to get this working correctly but I can't 我已经尝试了很多方法来使其正常工作,但是我做不到

Here is my problem: 这是我的问题:

I have an array of objects in javascript which looks something like this: 我在javascript中有一个对象数组,看起来像这样:

var myArrOfObjs = [];
var myObject1 = {};
myObject1.key = '1234';
myObject1.label = 'Richard Clifford';

myArrOfObjs.push( myObject1 );

and I need to do something like: 我需要做类似的事情:

if( !containsObject( myObject1, myArrOfOjbs ) ){
    // Do stuff
}

I need the containsObject function to check for key values within the found object (if any), so if containsObject( myObject1, myArrOfOjbs ) finds the object, I need to check to see if the key of that is the same as the one I am currently trying to push. 我需要containsObject函数来检查找到的对象内的键值(如果有的话),因此,如果containsObject( myObject1, myArrOfOjbs )找到该对象,则需要检查该键是否与我的键相同目前正在努力推动。

The reason I need it to check the keys is because I have tried this function which I found else where on StackOverflow, but it isn't quite working. 我需要它来检查键的原因是因为我尝试了此功能,但我在StackOverflow上的其他位置找到了该功能,但它无法正常工作。

function containsObject(obj, list) {
    var i;
    for (i = 0; i < list.length; i++) {
        if (list[i] == obj) {
            return true;
        }
    }

    return false;
}

It still pushes the object to the array even when it already contains the object. 即使它已经包含对象,它仍然将对象推入数组。

Please let me know if you need anything clearing up, I realise that it isn't the easiest post to read/understand. 如果您需要任何清理,请告诉我,我知道这不是最容易阅读/理解的帖子。

Thanks! 谢谢!

You need to change the equality test to compare the keys: 您需要更改相等性测试以比较键:

function containsObject(obj, list) {
    var i;
    for (i = 0; i < list.length; i++) {
        if (list[i].key === obj.key) {
            return true;
        }
    }

    return false;
}

I have got this function that gets added to the prototype-chain of the Array-Object, so you can just call list.hasObject(obj) . 我已经将此函数添加到Array-Object的原型链中,因此您只需调用list.hasObject(obj)

Array.prototype.hasObject = (
  !Array.indexOf ? function (o)
  {
    var l = this.length + 1;
    while (l -= 1)
    {
        if (this[l - 1] === o)
        {
            return true;
        }
    }
    return false;
  } : function (o)
  {
    return (this.indexOf(o) !== -1);
  }
);

small fiddle for this: http://jsfiddle.net/NE9kx/ 为此的小提琴: http : //jsfiddle.net/NE9kx/

Maybe it's not the answer you're looking for, but I wonder why don't use an Object instead of an Array, if you have a key: 也许这不是您要查找的答案,但是我想知道为什么如果有键,为什么不使用对象而不是数组?

var objectList = {};

var myObject { key : '1234', label : 'Richard Clifford' };

objectList[myObject.key] = myObject;

So if you want to iterate: 因此,如果要迭代:

for (var key in objectList) {
    if (objectList.hasOwnProperty(key)
        alert(key);
}

If you want to access to the object with a key given you have just to: 如果要使用给定的密钥访问对象,则只需执行以下操作:

alert(objectList['1234'].label);

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

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