상세 컨텐츠

본문 제목

Object.keys(), Object.values(), Object.entries()

JavaScript

by 폴리프레임 2024. 5. 6. 18:06

본문

반응형

Object.keys()

const layers = {
    OL : "OpenLayes",
    LF : "Leaflet"
};

const firstValue = Object.keys(layers)[0];
const firstKey = layers[Object.keys(layers)[0]];

console.log(firstValue);//OL
console.log(firstKey); // OpenLayes
console.log(Object.keys(layers)); // (2) ['OL', 'LF']


for (const key of Object.keys(layers)){
    console.log( key, layers[key])
} 
// OL OpenLayes
// LF Leaflet

for (const key of Object.keys(layers)){
    console.log(`${key}: ${layers[key]}`)
}
// OL: OpenLayes
// LF: Leaflet

console.log(firstValue);//OL
console.log(firstKey); // OpenLayes
console.log(Object.keys(layers)); // (2) ['OL', 'LF']

Objects.values()

const object1 = {
  a: 'somestring',
  b: 42,
  c: false,
};

console.log(Object.values(object1));
// Expected output: Array ["somestring", 42, false]

Object.entries()

const object1 = {
  a: 'somestring',
  b: 42,
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// Expected output:
// "a: somestring"
// "b: 42"

'JavaScript' 카테고리의 다른 글

npm i --save  (0) 2024.05.07
lodash.filter()  (0) 2024.05.07
CDN 과 module  (0) 2024.05.03
VectorSource, VectorLayer  (0) 2024.05.03
Array.from() 활용  (0) 2024.04.30

관련글 더보기