Given:

var peoples = [
  { "attr1": "bob", "attr2": "pizza" },
  { "attr1": "john", "attr2": "sushi" },
  { "attr1": "larry", "attr2": "hummus" }
];

Wanted:

对象索引,其中attr === value例如attr1 === "john"attr2 === "hummus"

Update: Please, read my question carefully, i do not want to find the object via $.inArray nor i want to get the value of a specific object attribute. Please consider this for your answers. Thanks!

推荐答案

The Functional Approach

All the cool kids are doing functional programming (hello React users) these days so I thought I would give the functional solution. In my view it's actually a lot nicer than the imperatival for and each loops that have been proposed thus far and with ES6 syntax it is quite elegant.

Update

There's now a great way of doing this called findIndex which takes a function that return true/false based on whether the array element matches (as always, check for browser compatibility though).

var index = peoples.findIndex(function(person) {
  return person.attr1 == "john"
});

With ES6 syntax you get to write this:

var index = peoples.findIndex(p => p.attr1 == "john");

The (Old) Functional Approach

TL;博士

如果你想找indexpeoples[index].attr1 == "john"使用:

var index = peoples.map(function(o) { return o.attr1; }).indexOf("john");

解释

Step 1

Use .map() to get an array of values given a particular key:

var values = object_array.map(function(o) { return o.your_key; });

上面的行会将您带到这里:

var peoples = [
  { "attr1": "bob", "attr2": "pizza" },
  { "attr1": "john", "attr2": "sushi" },
  { "attr1": "larry", "attr2": "hummus" }
];

到这里:

var values = [ "bob", "john", "larry" ];

Step 2

Now we just use .indexOf() to find the index of the key we want (which is, of course, also the index of the object we're looking for):

var index = values.indexOf(your_value);

Solution

We combine all of the above:

var index = peoples.map(function(o) { return o.attr1; }).indexOf("john");

或者,如果您更喜欢ES6语法:

var index = peoples.map((o) => o.attr1).indexOf("john");

演示:

var peoples = [
  { "attr1": "bob", "attr2": "pizza" },
  { "attr1": "john", "attr2": "sushi" },
  { "attr1": "larry", "attr2": "hummus" }
];

var index = peoples.map(function(o) { return o.attr1; }).indexOf("john");
console.log("index of 'john': " + index);

var index = peoples.map((o) => o.attr1).indexOf("larry");
console.log("index of 'larry': " + index);

var index = peoples.map(function(o) { return o.attr1; }).indexOf("fred");
console.log("index of 'fred': " + index);

var index = peoples.map((o) => o.attr2).indexOf("pizza");
console.log("index of 'pizza' in 'attr2': " + index);

Jquery相关问答推荐

使用jQuery筛选器搜索输入仅适用于单词的一部分

从文本区域获取值

未捕获的类型错误:无法读取未定义的属性toLowerCase

AngularJS 中的 ScrollTo 函数

jQuery.parseJSON 单引号与双引号

jQuery切换CSS?

jQuery 库中使用的设计模式

页面滚动后,jQuery可拖动在错误的位置显示助手

如何在 JavaScript 或 jQuery 中规范化 HTML?

专业的基于 jQuery 的 Combobox 控件?

在jQuery中添加ID?

如何检测 window.print() 完成

Bootstrap 4 文件输入

删除除一个之外的所有类

在jQuery中获取CSS规则的百分比值

jQuery.active 函数

更改 url 而不使用 javascript 重定向

如何禁用由子元素触发的 mouseout 事件?

Ajax 处理中的无效 JSON 原语

获取对象属性中的最小值/最大值的快速方法