Programming/JavaScript
[JavaScript] 배열을 객체로 만들기 : map(), Object.fromEntries()
aldrn29
2022. 8. 23. 22:05
함수로 배열을 객체로 만들기
1. map() 함수 이용하기
2. Object.fromEntries() 함수 이용하기
1. map() 함수 이용하기
const arr = [[1,2], [3,4], [5,6]];
let formattedArr = arr.map(function(a) {
let newArr = {};
newArr[a[0]] = a[1];
return newArr;
});
// formattedArr => (3) [{1: 2}, {3: 4}, {5: 6}]
2. Object.fromEntries() 함수 이용하기
const arr = [ ['name', 'Chloe'], ['age', 17], ['city', 'NewYork'] ];
const newObj = Object.fromEntries(arr);
// newObj => {name: 'Chloe', age: 17, city: 'NewYork'}
728x90
728x90