I would like to know the best way to find the index of an element in an array of objects with a certain value. The elements are objects, so to my knowledge, indexOf will not work.
Here is a generalization of the data:
I saw the findIndex method for arrays, so I initially wrote this.
It appears that the findIndex method is not supported in RPG Maker MV !
I get 'undefined is not a function' error messages in the console (v1.5.1).
Of course, I could write this as a standard 'for' loop.
My questions:
Here is a generalization of the data:
Code:
var myArray = [
{name: 'Jim',
value: 'foo'},
{name: 'Bob',
value: 'bar'},
{name: 'Pete',
value: 'foobar'}
];
I saw the findIndex method for arrays, so I initially wrote this.
Code:
var index = myArray.findIndex(function(i) {return i.value === 'bar'});
if (index >= 0) {
console.log('bar is found at index ' + index);
}
It appears that the findIndex method is not supported in RPG Maker MV !
I get 'undefined is not a function' error messages in the console (v1.5.1).
Of course, I could write this as a standard 'for' loop.
Code:
var index = -1;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].value === 'bar') {
index = i;
}
}
if (index >= 0) {
console.log('bar is found at index ' + index);
}
My questions:
- Is there a more elegant way to accomplish this that is allowed in RMMV?
- Do we know when RMMV will support this method? (ECMAScript 6)