TypeScript
-
[TypeScript] reduce 사용하기 (array to object)JavaScript 2023. 7. 6. 22:49
// before [ { name: "A", age: 16 }, { name: "B", age: 22 } ] // after { "A": 16 "B": 22 } const result = data.reduce((acc, cur) => { acc[cur.name] = cur.age return acc }, {}) 위와 같은 배열을 아래와 같이 객체로 변환하기 위해 reduce 메서드를 사용하게 되었다. 타입스크립트 쪽에서 다음과 같은 오류 메시지가 나타났다. Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a paramet..