按键查找数组中对象的索引

Find index of object in array by key

我有一个像这样的对象数组

myobj= [{"item1" : info in here},{"item2" : info in here}, {"item3" : info in here}]

我正在尝试修改一个,但我只知道它的密钥。我需要查明 item1 对象,以便我可以更改它的值(这些值是随机的,我不知道它们,所以我不能依赖它们)。

如果我能得到项目的索引,那就很容易了:myobj[index].value = "newvalue"

也许使用索引不是最好的方法,所以如果不是,我愿意接受其他想法。

我想我可以试试

myobj.objectVar

其中 objectVar 是我传递的密钥(例如 item1),但这不起作用,可能是因为它是一个变量?是否可以使用这样的变量?

如果有帮助,我也在使用 underscore.js。

您对解决方案的猜测不成立,因为您不是在访问单个对象,而是在访问对象数组,每个对象都有一个 属性.

要使用你现在得到的格式的数据,你需要遍历外部数组,直到找到包含你要查找的键的对象,然后修改它的值。

myobj= [{"item1" : info in here},{"item2" : info in here}, {"item3" : info in here}]

function setByKey(key, value) {
  myObj.forEach(function (obj) {
    // only works if your object's values are truthy
    if (obj[key]) {
      obj[key] = value;
    }
  });
}

setByKey('item1', 'new value');

当然,更好的解决方案是停止使用单个 属性 对象的数组,而只使用一个具有多个属性的对象:

myobj= {"item1" : info in here, "item2" : info in here, "item3" : info in here};

现在,您可以只需使用myObject.item1 = "some new value"就可以了。

试试这个代码:

function changeObj( obj, key, newval )
{
    for( var i=0, l=obj.length; i<j; i++)
    {
        if( key in obj[i] )
        {
            obj[i] = newval;
            return;
        }
    }
}
var myObjArray= [{"item1" : "info in here"},{"item2" : "info in here"}, {"item3" : "info in here"}]

查找数组中的对象并将新值添加到对象:

myObjArray.forEach(function(obj) {
    for(var key in obj) {
         // in case you're matching key & value
         if(key === "item1") {
             obj[key] = "update value";
             // you can even set new property as well
             obj.newkey = "New value";
         }
    }
 });

你可以写一个像这样的函数,

function getElementsHavingKey(key) {
        var objectsHavingGivenKey = [];

        //loop through all the objects in the array 'myobj'

        myobj.forEach(function(individualObject) {

            //you can use 'hasOwnProperty' method to find whether the provided key 
            // is present in the object or not

            if(individualObject.hasOwnProperty(key)) {

                // if the key is present, store the object having the key 
                // into the array (many objects may have same key in it)

                objectsHavingGivenKey.push(individualObject);
            }
        });

        // return the array containing the objects having the keys

        return objectsHavingGivenKey;
}

如果您只想获取具有给定键的元素的索引

你可以这样做,

function getIndexesOfElementsHavingKey(key) {
        var objectsHavingGivenKey = [];

        //loop through all the objects in the array 'myobj'

        myobj.forEach(function(individualObject, index) {

            //you can use 'hasOwnProperty' method to find whether the provided key 
            // is present in the object or not

            if(individualObject.hasOwnProperty(key)) {

                //push index of element which has the key

                objectsHavingGivenKey.push(index);
            }
        });

        // returns the array of element indexes which has the key
        return objectsHavingGivenKey;
}

您可以使用它们的索引访问相同的对象,甚至是原始对象内部的对象。

您要找的是这样的吗:

var otherObj = [{"oitem":"oValue"}];
var myobj= [{"item1" : otherObj},{"item2" : "2"}, {"item3" : "tesT"}];
myobj[0].item1[0].oitem = "newvalue"; 
alert(myobj[0].item1[0].oitem);