对象数组中的indexOf方法? [英] indexOf method in an object array?

查看:311
本文介绍了对象数组中的indexOf方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取包含对象的数组索引的最佳方法是什么?

What's the best method to get the index of an array which contains objects?

想象一下这种情况:

var hello = {
    hello: 'world',
    foo: 'bar'
};
var qaz = {
    hello: 'stevie',
    foo: 'baz'
}

var myArray = [];
myArray.push(hello,qaz);

现在我希望 indexOf hello 属性为'stevie'的对象,在本例中为 1

Now I would like to have the indexOf the object which hello property is 'stevie' which, in this example, would be 1.

我是JavaScript的新手,我不知道是否有一个简单的方法或者我是否应该构建自己的函数要做到这一点。

I'm pretty newbie with JavaScript and I don't know if there is a simple method or if I should build my own function to do that.

推荐答案

我认为你可以使用 map 功能:

I think you can solve it in one line using the map function:

pos = myArray.map(function(e) { return e.hello; }).indexOf('stevie');

这篇关于对象数组中的indexOf方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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