在复杂数组上使用 indexOf 时遇到问题 [英] Trouble using indexOf on a complex array

查看:54
本文介绍了在复杂数组上使用 indexOf 时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
对象数组中的indexOf方法?

我有一个遵循以下格式的 javascript 数组:

I have a javascript array which follows this format:

var arrayName = [
{id: "a", gender: "man",   item: "stuff"},
{id: "b", gender: "woman", item: "stuff"},
{id: "c", gender: "man",   item: "stuff"},
{id: "d", gender: "man",   item: "stuff"}
];

有没有一种方法可以使用 array.indexOf 来查找数组中的索引,例如,当我知道id"变量时.

Is there a way that I can use array.indexOf to find an index in the array, when for example I know the "id" variable.

例如我试过;

var position = arrayName.indexOf("b");
arrayName[position].gender = "man"

目前我正在使用;

for(var i=0; i<arrayName.length; i++) {
   if(arrayName[i].id == "b"){
       arrayName[i].gender = "man";
   }
}

第二种技术有效,但我使用的实际数组有 150 个条目和 10 个条目,因此当我知道要编辑的条目的id"时,循环遍历它似乎非常浪费.如果我可以使用 indexOf 将是一种更简洁的方法.

This second technique works but the actual array I am using has 150 entries and 10 items in each entry so looping through it all seems very wasteful when I know the "id" of the entry I want to edit. indexOf would be a much cleaner approach if I can get it working.

推荐答案

您对 indexOf 的调用正在根据字符串 b 检查数组中的对象,该字符串永远不会工作.

Your call to indexOf is checking the objects in your array against the string b, which will never work.

遍历数组元素以找到正确的 id 是一个简单的解决方案.

Looping through the array elements to find the right id is a simple solution.

或者,您可以创建自己的 indexOf 函数:

Or, you could make your own indexOf function:

Array.prototype.indexOfId = function(id) {
    for (var i = 0; i < this.length; i++)
        if (this[i].id === id)
            return i;
    return -1;
}

    var arrayName = [
      { id: "a", gender: "man", item: "stuff" },
      { id: "b", gender: "woman", item: "stuff" },
      { id: "c", gender: "man", item: "stuff" },
      { id: "d", gender: "man", item: "stuff" }];

    var position = arrayName.indexOfId("b");

    alert(position);  //alerts 1

这是一个小提琴

编辑

如果您想将数组元素与任何属性进行比较,请按照以下方法进行操作(请注意,我使用 [] 语法来获取任意属性)

If you want to compare your array elements to any property, here's how you do it (note that I'm using [] syntax to get an arbitrary property)

    Array.prototype.indexOfField = function (propertyName, value) {
        for (var i = 0; i < this.length; i++)
            if (this[i][propertyName] === value)
                return i;
        return -1;
    }

    var position = arrayName.indexOfField("id", "b");
    alert(position);  //alerts 1

    position = arrayName.indexOfField("gender", "man");
    alert(position); //alerts 0

<小时>

或者,如果您不想弄乱 Array 的原型,并且不介意使用旧浏览器中不存在的功能,则可以使用 Array 的 filter 功能

var position = arrayName.indexOf(arrayName.filter(function (val) {
      return val.id === "b";
})[0]);

请注意,IE8 中不存在此功能,因此要支持此浏览器,您必须获取 来自 MDN 的垫片

Note that this function is not present in IE8, and so to support this browser you'd have to grab the shim from MDN

EDIT - 哎呀,indexOf 对旧浏览器也不友好.如果您选择第二部分代码并希望支持 IE,您还必须为 indexOf 从这里

EDIT - oops, indexOf isn't friendly toward old browsers either. If you opt for this second bit of code and wanted to support IE, you'd also have to grab the shim for indexOf from here

这篇关于在复杂数组上使用 indexOf 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆