Home > OS >  acces to previous element in element of arrays
acces to previous element in element of arrays

Time:05-31

i explain my question with an example :

    myArray=[10,15,42,74,65,2,7,9]

so, how can i acces to All elements for example before element '65' :

    newArray=[10,15,42,74]

CodePudding user response:

You can use Array.slice(start, length) to get just part of your original array, and Array.indexOf() to get index of the number 65.

let array = [10, 15, 42, 74, 65, 2, 7, 9]

// 0, because you want to start on the first index (10)
let newArray = array.slice(0, array.indexOf(65))
  • Related