+ 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.

18th Oct 2022, 12:31 AM
Raja Rawat
Raja Rawat - avatar
2 Answers
+ 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
18th Oct 2022, 2:41 AM
Ipang
+ 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)
18th Oct 2022, 2:39 AM
Niththish
Niththish - avatar