【发布时间】:2021-12-15 15:13:18
【问题描述】:
how can i push the index on one line like this->[1,2,2,3,2]
instead of this->[1][2][2][3][2]
``` let array = [1, [2, 3, [4, 5, ["six", "seven", 6666, [8, 9, [10]]]]]];```
function findLastElement (arr){
for (let [index, element] of arr.entries()){
let array = []
if(typeof element === "object"){
findLastElement(element)
array.push(index)
console.log(array)
}
}
}
console.log(findLastElement(array))
- 我添加了这一行
let array = []
2.我需要将它推入数组
array.push(index)
2.在控制台中我应该只有这样的索引 [1,2,2,3,2],但现在我在不同的数组中都有索引 [1][2].....
- 我可以用这个例子吗?
【问题讨论】:
-
对不起,我不明白。请edit 问题并尽可能清楚地解释:(1)您要实现的目标,(2)实际输入数据或源数据,(3)您获得的输出,以及(4)输出你想要的。该问题目前包含 3 个不同的数组,但我无法根据 (2)、(3) 和 (4) 判断哪个是哪个。
-
在控制台中我应该只有这样的索引 [1,2,2,3,2],但现在我在不同的数组中都有索引 [1][2].....跨度>
标签: javascript arrays function recursion