[英]Javascript indexOf on an array of objects
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"?我如何获得“蓝色”的索引?
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.这与上面相同,但更小。
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.除非您想涉及第三方库,否则没有“干净”的方式。 Underscore is a good one for stuff like this.
下划线对于这样的东西来说是一个很好的选择。
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"
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).这不是多维数组(甚至不是锯齿状数组,它在 Javascript 中用于代替多维数组,因为它们不存在)。 That is an array of objects.
那是一个对象数组。
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
包含值1
,与你给定的例子。
You can use .findIndex() method您可以使用.findIndex() 方法
In your case在你的情况下
var findeMe = "blue";
myArray.findIndex(function(row){
return row.color == findMe;
});
I hope it help.我希望它有所帮助。
I'm guessing you mean from your example to return "1"?我猜你的意思是从你的例子中返回“1”? Here is a function for you...
这里有一个功能给你...
<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>
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'))
In my case, I have built my own functions because I wanted the loop to stop at the first element found.就我而言,我构建了自己的函数,因为我希望循环在找到的第一个元素处停止。
That's why the solutions with map were not working to this case as they are creating a new array.这就是为什么带有 map 的解决方案在创建新数组时不适用于这种情况。
var i = 0;
while (i < myArray.length) {
if (myArray[i].color == 'blue') {
return i;
}
i++
}
return -1
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 并没有给你很多东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.