본문 바로가기

개발/ES6

Lodash Zip() : Reduce로 구현해보기

const originList = [
  [1, 2],
  [3, 4, 5],
  [6, 7, 8, 9],
];

const reduceZipResult = originList.reduce((acc, cur, idx) => {
  for (let i = 0; i < cur.length; i++) {
    let el = acc[i];
    if (el === undefined) {
      el = [];
      for (let j = 0; j < i; j++) {
        el[j] = undefined;
      }
      //el = Array(cur.length).fill(undefined);
    }
    el[idx] = cur[i];
    acc[i] = el;
  }

  return acc;
}, []);

console.log(reduceZipResult);