1let obj = {
2 key1: "value1",
3 key2: "value2",
4 key3: "value3"
5}
6
7Object.keys(obj).forEach(key => {
8 console.log(key, obj[key]);
9});
10// key1 value1
11// key2 value2
12// key3 value3
13
14// using for in - same output as above
15for (let key in obj) {
16 let value = obj[key];
17 console.log(key, value);
18}
1for (var property in object) {
2 if (object.hasOwnProperty(property)) {
3 // Do things here
4 }
5}
6
1for (const [fruit, count] of entries) {
2 console.log(`There are ${count} ${fruit}s`)
3}
4
5// Result
6// There are 28 apples
7// There are 17 oranges
8// There are 54 pears
9
1const fruits = {
2 apple: 28,
3 orange: 17,
4 pear: 54,
5}
6
7const entries = Object.entries(fruits)
8console.log(entries)
9// [
10// [apple, 28],
11// [orange, 17],
12// [pear, 54]
13// ]
14
1const fruits = {
2 apple: 28,
3 orange: 17,
4 pear: 54,
5}
6
7const values = Object.values(fruits)
8console.log(values) // [28, 17, 54]
9