데이터 타입 변경하기
author: Dev.ian
createdAt: 2024-05-28
updatedAt: 2024-08-25
- Map -> Array
-
example data
// map
const map: Map<string, string> = new Map<string, string>()
.set('apple', 'ios')
.set('google', 'android')
.set('ms', 'windows');
Array.from()
-
example code
const arr = Array.from(map, ([name, os]) => ({name, os}));
-
custom(Key 이름 지정)
const arr = Array.from(map, ([key, value]) => ({name: key, os: value}));
[{
"name": "apple",
"os": "ios"
}, {
"name": "google",
"os": "android"
}, {
"name": "ms",
"os": "windows"
}] -
custom
const arr = Array.from(map, ([key, value]) => ({[key]: value}));
[{
"apple": "ios"
}, {
"google": "android"
}, {
"ms": "windows"
}]
[...]
-
Array.from()
과 동일하게 사용 가능const arr = [...map].map(([name, os]) => ({name, os}));
const arr = [...map].map(([key, value]) => ({name: key, os: value}));
const arr = [...map].map(([key, value]) => ({[key]: value}));
- Set -> Array
-
example data
// set
const companies: Set<string> = new Set<string>()
.add('apple')
.add('google')
.add('ms');
Array.from()
const arr = Array.from(companies);
["apple", "google", "ms"]
[...]
const arr = [...companies];
["apple", "google", "ms"]
- Array -> Map
-
example data
// array
const arr = ['apple', 'google', 'ms']; -
code
new Map<K,V>([[key1, value1], [key2, value2], ... , [keyN, valueN]])
const map = new Map<string, number>(
arr.map((item, idx) => [item, idx])
);Map (3) {"apple" => 0, "google" => 1, "ms" => 2}
- Array -> Set
-
example data
// array
const arr = ['apple', 'google', 'ms']; -
code
new Set<T>([item1, item2, ... , itemN])
const companies = new Set<string>(arr);
Set (3) {"apple", "google", "ms"}