js 能像 Python 一样遍历嵌套的列表或元组吗?
- 0次
- 2021-08-10 01:26:58
- idczone
python 里可以 for a,b,c,d in list:
还可以 for a,b in zip(list1,list2):
js 只能一层循环套一层循环吗?感觉层数多了会晕
ES6 有 destructuring assignment
ES2019 有 Array.prototype.flat()
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators
不知道标准库有没有,但是既然有上面文档的支持,自己实现一个也不难,,,,
for (const [a,b,c,d] of list)
const zip = (a, b) => a.map((e, i) => [e, b[i]])
for (const [a, b] of zip(list1, list2))
array=[[1 ,2], [3, 4]]
for ([k, v] of array){
console.log(k, v)
}
---
1 2
3 4
那些文档看不懂啊,术语太多了。
谢谢 可以的