1'use strict';
2// ECMAScript 2017
3const object = {'a': 1, 'b': 2, 'c' : 3};
4for (const [key, value] of Object.entries(object)) {
5 console.log(key, value);
6}
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}
1var p = {
2 "p1": "value1",
3 "p2": "value2",
4 "p3": "value3"
5};
6
7// for-in
8for (var key in p) {
9 if (p.hasOwnProperty(key)) {
10 console.log(key + " -> " + p[key]);
11 }
12}
13
14// for-of with Object.keys()
15for (var key of Object.keys(p)) {
16 console.log(key + " -> " + p[key])
17}
18
19// Object.entries()
20for (let [key, value] of Object.entries(p)) {
21 console.log(`${key}: ${value}`);
22}
1const fruits = { apple: 28, orange: 17 }
2
3for(key in fruits){
4 console.log(key)
5}
1var p = {
2 "p1": "value1",
3 "p2": null,
4 "p3": "value3"
5};
6
7for (var key in p) {
8 if (p.hasOwnProperty(key)) {
9 console.log(key + " -> " + p[key]);
10 }
11}