Javascript 对象数组上的Javascript indexOf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8313350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript indexOf on an array of objects
提问by redconservatory
if I have an array like:
如果我有一个像这样的数组:
var myArray = [
{
'color':'red',
'name': 'redName'
},
{
'color':'blue',
'name': 'blueName'
},
{
'color':'green',
'name': 'greenName'
},
{
'color':'yellow',
'name': 'yellowName'
},
];
How do I get the index of say, "blue"?
我如何获得“蓝色”的索引?
回答by Gustavo Rodrigues
If you're already using ECMAScript 5 in your code you can use that:
如果您已经在代码中使用 ECMAScript 5,则可以使用它:
myArray
.map(function (element) {return element.color;})
.indexOf('blue');
Note that the support to these functions is a quite limited (they don't work on Internet Explorer 8).
请注意,对这些功能的支持非常有限(它们不适用于 Internet Explorer 8)。
Also, if you're in the future, and you're using ES6 you can do that:
另外,如果你在未来,并且你正在使用 ES6,你可以这样做:
myArray.map((el) => el.color).indexOf('blue');
That's the same as above, but smaller.
这与上面相同,但更小。
回答by John Strickler
for(var i = 0; i < myArray.length; i++) {
if(myArray[i].color === 'blue') {
return i;
}
}
There's no "clean" way unless you want to involve a third-party library. Underscoreis a good one for stuff like this.
除非您想涉及第三方库,否则没有“干净”的方式。 下划线对于这样的东西来说是一个很好的选择。
回答by Aleksandr Strutynskyy
I know this is super old but Javascript es6 has an awesome method Array.prototype.findIndex(), so you could just do:
我知道这已经很老了,但是 Javascript es6 有一个很棒的方法Array.prototype.findIndex(),所以你可以这样做:
let index = myArray.findIndex((item) => item.color === 'blue');
// value of index will be "1"
回答by Guffa
That's not a multi-dimensional array (or even a jagged array, which is used instead of multi-dimensional arrays in Javascript as they don't exist). That is an array of objects.
这不是多维数组(甚至不是锯齿状数组,它在 Javascript 中用于代替多维数组,因为它们不存在)。那是一个对象数组。
You can loop through the items in the array:
您可以遍历数组中的项目:
var index;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].color == 'blue') {
index = i;
break;
}
}
Now the variable index
contains the value 1
, with your given example.
现在变量index
包含 value 1
,与你给定的例子。
回答by Webby
You can use .findIndex() method
您可以使用.findIndex() 方法
In your case
在你的情况下
var findeMe = "blue";
myArray.findIndex(function(row){
return row.color == findMe;
});
I hope it help.
我希望它有所帮助。
回答by mplungjan
For example like this:
例如像这样:
Array.prototype.objIndexOf = function(val) {
var cnt =-1;
for (var i=0, n=this.length;i<n;i++) {
cnt++;
for(var o in this[i]) {
if (this[i].hasOwnProperty(o) && this[i][o]==val) return cnt;
}
}
return -1;
}
var myArray = [
{
'color':'red',
'name': 'redName'
},
{
'color':'blue',
'name': 'blueName'
},
{
'color':'green',
'name': 'greenName'
},
{
'color':'yellow',
'name': 'yellowName'
},
];
alert(myArray.objIndexOf('blue'))
回答by Crayon Violent
I'm guessing you mean from your example to return "1"? Here is a function for you...
我猜你的意思是从你的例子中返回“1”?这里有一个功能给你...
<script type='text/javascript'>
var myArray = [
{
'color':'red',
'name': 'redName'
},
{
'color':'blue',
'name': 'blueName'
},
{
'color':'green',
'name': 'greenName'
},
{
'color':'yellow',
'name': 'yellowName'
},
];
function getIndexOf(a,v) {
var l = a.length;
for (var k=0;k<l;k++) {
if (a[k].color==v) {
return k;
}
}
return false;
}
alert (getIndexOf(myArray,'blue'));
</script>
回答by Dave Newton
Iterate over the array, looking for the value inside each element object. Raw JavaScript doesn't give you a whole lot to work with.
遍历数组,查找每个元素对象内的值。原始 JavaScript 并没有给你很多东西。