如何在javascript数组中按键和值查找对象的索引 [英] How to find index of an object by key and value in an javascript array

查看:125
本文介绍了如何在javascript数组中按键和值查找对象的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于:

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

通缉:

对象索引 attr === value 例如 attr1 ===john attr2 ===鹰嘴豆泥

更新:
请阅读我的问题仔细,我不想通过$ .inArray找到对象,也不想获取特定对象属性的值。请考虑这个以获得答案。谢谢!

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!

推荐答案

功能方法



所有酷孩子都在行动编程(你好React用户)这些天所以我想我会给出功能性的解决方案。在我看来它实际上比迄今为止提出的和 每个循环的阻碍好很多语法非常优雅。

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.

现在有一种很好的方法可以做到这一点,叫做 findIndex 根据数组元素是否匹配(一如既往,检查浏览器),它接受一个函数返回 true / false 虽然兼容性。

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"
}

使用ES6语法,您可以写下:

With ES6 syntax you get to write this:

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






(旧)功能方法



TL; DR



如果你e寻找 index 其中 peoples [index] .attr1 ==john使用:

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



说明



第1步

使用 .map() 获取给定特定键的值数组:

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" ];

第2步

现在我们只使用 .indexOf() 查找我们想要的键的索引(当然,也是我们要查找的对象的索引):

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);

解决方案

我们结合以上所有:

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

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

Or, if you prefer ES6 syntax:

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






演示:




Demo:

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);

这篇关于如何在javascript数组中按键和值查找对象的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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