一个复杂的阵列上使用的indexOf麻烦 [英] Trouble using indexOf on a complex array

查看:100
本文介绍了一个复杂的阵列上使用的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"

目前我使用的那一刻;

At the moment I am using;

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

这第二种技术工作,但我使用的是实际的数组有150项,并在每个条目通过这一切如此循环10个项目似乎很浪费的,当我知道我要编辑的条目的身份证。的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


或者,如果你不想惹阵列的原型,你不介意使用功能在旧的浏览器并不present,你可以使用Array的过滤器函数

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

请注意,此功能在IE8 present,因此支持这一浏览器你必须抓住的从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

修改 - 哎呀,的indexOf是不是对旧的浏览器友好无论是。如果您选择code的第二位和想支持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天全站免登陆