js 一個或多個一維數組,算出元素之間相互組合的所有情況
阿新 • • 發佈:2018-01-23
urn each pre 相互 get function post data nta
// 數據源 var target = { state1: [‘1‘, ‘2‘], state2: [‘01‘, ‘02‘, ‘03‘], state3: [‘001‘,‘002‘] } stackSituation(target) function stackSituation (data) { var targetKeys = Object.keys(data) var target = faker = [] if (targetKeys.length) { target= targetKeys.map((key) => data[key]) faker = target[0] } target.forEach((itemArr, index, currentArr) => { faker = js(faker, currentArr[index + 1]) }) function js(arr1, arr2){ var arr1 = arr1 || [] var arr2 = arr2 || [] faker= [] for(var i = 0;i < arr1.length; i++){ if (!arr2.length) { faker.push(arr1[i]) } else { for(var j = 0; j < arr2.length; j++){ faker.push(arr1[i] + ‘_‘ +arr2[j]); } } }return faker } var result = faker.map((item) => item.split(‘_‘)) console.table(result) return result }
js 一個或多個一維數組,算出元素之間相互組合的所有情況