728x90
    
    
  반응형
    
    
    
  Object를 Array로 변환하는 메서드들에 대해 알아보자.
Object.keys() : object의 property names을 배열로 반환
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
Object.keys(object1).map(key => (
	console.log(key)
)) output :
> Array ["a", "b", "c"]
> "a"
> "b"
> "c"Object.values() : object의 property values을 배열로 반환
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]
Object.values(object1).map(value => (
	console.log(value)
)) output :
> Array ["somestring", 42, false]
> "somestring"
> 42
> falseObject.entries() : object의 [key, value] 쌍이 담긴 배열을 반환
const object1 = {
  a: 'somestring',
  b: 42
};
console.log(Object.entries(object1));
for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}
Object.entries(object1).map(([key, value]) => (
	console.log(key, value)
)) 
// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteedoutput:
> Array [Array ["a", "somestring"], Array ["b", 42]]
> "a: somestring"
> "b: 42"
> "a" "somestring"
> "b" 42728x90
    
    
  반응형
    
    
    
  'JavaScript' 카테고리의 다른 글
| JavaScript] Searching: getElement*, querySelector* (0) | 2022.11.04 | 
|---|---|
| JavaScript] DOM Navigation (0) | 2022.11.03 | 
| JavaScript] DOM tree (0) | 2022.11.03 | 
| JavaScript] Browser environment, DOM, BOM (0) | 2022.11.03 | 
| JavaScript] 웹에서 Apple Login 구성하기 (2) | 2021.10.13 | 
 
										
									 
										
									 
										
									 
										
									
댓글