flatten, unflatten

sang hyeok Lee·2022년 4월 22일
0

flatten

Flatten은 array 구조 안에 또 다른 array를 인덱스로 가질 때, array를 평평하게 만드는 작업이다.
예를 들어 배열안에 배열이 있는 이중배열구조를 하나의 배열로 배열로 만들어 주는 것이다.

const arr = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

메스드 flat를 사용해서 배열안에 배열이 있는 구조릏 평탄하게 만들어주고 있다.

unflatten

unflatten은 flatten과 반대로 비평탄화를 하는 것이다

const unflatten = require('unflatten')
unflatten({
  'a.b.c': 'd'
})
/*
{
  a: {
    b: {
      c: 'd'
    }
  }
}

unflatten함수 안에 객체를 넣어주면 이 객체를 위에 예시와 같이 객체 안에 객체의 형태로 비편탄화를 시켜주는 함수이다.

profile
개발자 되기

0개의 댓글