鍍金池/ 問答/Python  C  C++/ js數(shù)組遞歸循環(huán)

js數(shù)組遞歸循環(huán)

循環(huán)數(shù)組

arr = [
   [1, 2],
   [3, 4],
]

輸出[13, 14, 23, 34];

循環(huán)數(shù)組

arr = [
   [1, 2],
   [3, 4],
   [5, 6],
]

輸出[135, 136, 145, 146, 235, 236, 245, 246]

循環(huán)的數(shù)組長度未知,數(shù)組的每個子數(shù)組長度未知。

要寫一個通用的方法?

回答
編輯回答
背叛者

用數(shù)組的reduce方法
let arr = [[1, 2], [3, 4], [5, 6]]
let list = []
arr.reduce((pre, current, index, arr) => {

list = []
for (let i = 0; i < pre.length; i++) {
    for (let j = 0; j < current.length; j++) {
        list.push(+(pre[i] + '' + current[j]))
    }
}
return list

})

console.log(list)

2018年5月6日 12:28