EN
JavaScript - iterate key/value pair from JSON
0 points
In this article, we would like to show you how to iterate key/value pairs from JSON in JavaScript.
In this example, we use Object.keys()
method to get an array of object keys. Then we use forEach()
to loop through the array of keys and print them in the console.
xxxxxxxxxx
1
const json = `
2
{
3
"a": "value1",
4
"b": "value2",
5
"c": "value3"
6
}
7
`;
8
9
const data = JSON.parse(json);
10
const keys = Object.keys(data);
11
12
keys.forEach((key) => console.log('key: ' + key + ', value: ' + data[key]));