+ 1
How to get the index of an subarray.
https://code.sololearn.com/WB99wCh1S94S/?ref=app How to get the index of an subarray which contain num=3.
2 ответов
+ 4
First off, you need to fix the objects' initialization. Object's property assignment uses a colon ':' not an assignment operator '='
Example:
{ ato : "anything", num : 42 }
To find index of an array element we can use findIndex() method
console.log( array.findIndex( element => element.num == 3 ) );
Reference:
https://developer.mozilla.org/id/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
+ 3
Use findIndex method
const array = [
{ato:"anything1",num:11},
{ato:"anything2",num:3},
{ato:"anything3",num:7}
]
const index = array.findIndex(arr => arr.num===3)
console.log(index)