온라인 쇼핑몰에서 어느 상품의 옵션 데이터를 다음과 같이 불러올 수 있다.
100번, 200번, 300번... 100번대 숫자를 대분류로 지정하고 있으며
101, 102, 103...을 소분류로 지정하고 있다.let list = [ [ {product_uid: 1, option_id: 101, big_category: 'color', small_category:'red'} ,{product_uid: 1, option_id: 102, big_category: 'color', small_category:'blue'} ,{product_uid: 1, option_id: 103, big_category: 'color', small_category:'yellow'}] [ {product_uid: 1, option_id: 201, big_category: 'size', small_category:'M'} ,{product_uid: 1, option_id: 202, big_category: 'size', small_category:'L'} ,{product_uid: 1, option_id: 203, big_category: 'size', small_category:'XL'} ], [ {product_uid: 1, option_id: 301, big_category: 'package', small_category:'packaging'} ,{product_uid: 1, option_id: 302, big_category: 'package', small_category:'non_packaging'} ] ]
대분류별로 옵션값을 하나씩 가져와 조합하면 하나의 상품으로 판매할 수 있다.
예를 들어 101, 201, 301값을 조합하면 다음과 같은 상품이 생성된다.{option_id: 101/201/301, cartegory_name:'red/M/packaging'}
대분류 안의 모든 옵션값을 이용해서 조합할 수 있는 모든 경우의 수 데이터를 출력하세요.
- 옵션을 가지고 있는 상품번호는 product_uid이다.
- 대분류 옵션 개수는 최소 1개 이상이다.
- 소분류 옵션 개수는 최소 1개 이상이다.
- 소분류는 대분류 옵션 안에 속해있다.
let input = [ [ {product_uid: 1, option_id: 101, big_category: 'color', small_category:'red'} ,{product_uid: 1, option_id: 102, big_category: 'color', small_category:'blue'} ,{product_uid: 1, option_id: 103, big_category: 'color', small_category:'yellow'}], [ {product_uid: 1, option_id: 201, big_category: 'size', small_category:'M'} ,{product_uid: 1, option_id: 202, big_category: 'size', small_category:'L'} ,{product_uid: 1, option_id: 203, big_category: 'size', small_category:'XL'} ], [ {product_uid: 1, option_id: 301, big_category: 'package', small_category:'packaging'} ,{product_uid: 1, option_id: 302, big_category: 'package', small_category:'non_packaging'} ], ] let output = [ {option_id: 101/201/301, cartegory_name:'red/M/packaging'} ,{option_id: 101/201/302, cartegory_name:'red/M/non_packaging'} ,{option_id: 101/202/301, cartegory_name:'red/L/packaging'} ,{option_id: 101/202/302, cartegory_name:'red/L/non_packaging'} ,{option_id: 101/203/301, cartegory_name:'red/XL/packaging'} ,{option_id: 101/203/302, cartegory_name:'red/XL/non_packaging'} ,{option_id: 102/201/301, cartegory_name:'blue/M/packaging'} ,{option_id: 102/201/302, cartegory_name:'blue/M/non_packaging'} ,{option_id: 102/202/301, cartegory_name:'blue/L/packaging'} ,{option_id: 102/202/302, cartegory_name:'blue/L/non_packaging'} //...색상 3, 사이즈 3, 포장여부 2 총 18개의 상품 정보 생성 ]
function solution(arr){
function recursion(bigIdx, smallIdx, tempList){
if(bigIdx == arr.length -1){
for(let i = 0; i < arr[bigIdx].length; i++){
let list = tempList
list.push(i)
let obj = {product_uid: ''
, option_combination_id:''
, option_combination_name:''
, is_soldout:0, option_combination_price:0
, option_combination_stock:1000}
for(let j = 0; j < list.length; j++){
obj['product_uid'] = arr[j][list[j]]['product_uid']
obj['option_combination_id'] += arr[j][list[j]]["option_id"]+'/'
obj['option_combination_name'] += arr[j][list[j]]["small_category"]+'/'
obj['is_soldout'] = 0
obj['option_combination_price'] += arr[j][list[j]]["option_price"]
}
list.pop()
console.log(obj)
//결과값을 db로 보내면 된다.
}
}else{
for(let i = 0; i < arr[bigIdx].length; i++){
let list = tempList
list.push(i)
recursion(bigIdx+1, i, list)
list.pop()
}
}
}
recursion(0,0,[])
}
solution(input);