根据其性质数组的元素越来越指数 [英] Getting index of an array's element based on its properties

查看:125
本文介绍了根据其性质数组的元素越来越指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的JavaScript数组是这样的:

  myarray的无功= [{...},{...},{...}];

每个对象都有独特的 ID 其他属性中:

  {
  ID:4,
  property1:'东西',
  property2:'东西'
}

我怎样才能得到一个特定对象的索引数组中,如果我只知道它的 ID 属性?所以,如果我知道, myArray的[X] .ID == 4 ,我怎么能找到 X


解决方案

  VAR指数= myArray.map(功能(EL){
  返回el.id;
})的indexOf(4)。

有关IE 9以下版本,<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map\">map需要补丁,或者只是使用一个循环。

I have a JavaScript array of objects like this:

var myArray = [{...}, {...}, {...}];

Each object has unique id among other properties:

{ 
  id: 4,
  property1: 'something',
  property2: 'something'
}

How can I get an index of a particular object in that array, if I only know its id property? So if I know that myArray[x].id == 4, how can I find x?

解决方案

var index = myArray.map(function(el) {
  return el.id;
}).indexOf(4);

For IE below version 9, map need a patch, or just use a loop.

这篇关于根据其性质数组的元素越来越指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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