JS gets the key and value of an object

Get the key value of the object

The Object.keys(obj) method returns an array containing all key values ​​in the obj object

Object.keys(obj)
const data = { '2022-11-18': '10', '2022-11-19': '17', '2022-11-20': '21', '2022-11-21': '7' };
console.log(Object.keys(data)); // ["2022-11-18", "2022-11-19", "2022-11-20", "2022-11-21"]

Get the value of the object

The Object.value(obj) method returns an array containing all value values ​​in the obj object

Object.values(obj)
const data = { '2022-11-18': '10', '2022-11-19': '17', '2022-11-20': '21', '2022-11-21': '7' };
console.log(Object.values(data)); // ["10", "17", "21", "7"]

Get the key value and value value at the same time

for in loops through objects

If you want to traverse all the keys (keys) and values ​​(values) in an object obj, you generally use the following method

const data = { '2022-11-18': '10', '2022-11-19': '17', '2022-11-20': '21', '2022-11-21': '7' };
for (let key in data) {
    console.log(key, data[key]);
}

// 2022-11-18 10
// 2022-11-19 17
// 2022-11-20 21
// 2022-11-21 7

Guess you like

Origin blog.csdn.net/AdminGuan/article/details/127990937