let array = [1, [2, 3, [4, 5, ["six", "seven", 6666, [8, 9, [10]]]]]] //1.find last element //2. index way like in **console.log(array[1][2][2][3][2][0]** but should print // [1][2][2][3][2][0] or 1,2,2,3,2,0en esta función encuentro el último elemento ahora no puedo encontrar la segunda pregunta (debería ser una función recursiva)
function findLastElement (arr){ for (let element of arr ){ if(typeof element === "object"){ findLastElement(element) console.log(element) } } } findLastElement(array)Puede usar una función recursiva para tomar siempre el último índice y continuar si el último elemento también es una matriz:
const getLessIndexes = arr => { const last = arr.length - 1 return [ last, ...Array.isArray(arr[last]) ? getLessIndexes(arr[last]) : [] ] } const array = [1, [2, 3, [4, 5, ["six", "seven", 6666, [8, 9, [10]]]]]] const result = getLessIndexes(array) console.log(result) let array = [1, [2, 3, [4, 5, ["six", "seven", 6666, [8, 9, [10]]]]]]; function findLastElement (arr){ for (let [index, element] of arr.entries() ){ if(typeof element === "object"){ findLastElement(element) console.log(element) console.log(index) } } } console.log( findLastElement(array) )Puedes usar Array.entries()
function findLastElement (arr){ for (let [index, element] of arr.entries() ){ if(typeof element === "object"){ findLastElement(element) console.log(element) console.log(index) } } }