1myObject ={a:1,b:2,c:3}
2
3//es6
4Object.entries(myObject).forEach(([key, value]) => {
5 console.log(key , value); // key ,value
6});
7
8//es7
9Object.keys(myObject).forEach(key => {
10 console.log(key , myObject[key]) // key , value
11})
1const obj = {
2 name: 'Jean-Luc Picard',
3 rank: 'Captain'
4};
5
6// Prints "name Jean-Luc Picard" followed by "rank Captain"
7Object.keys(obj).forEach(key => {
8 console.log(key, obj[key]);
9});
1const object1 = {
2 a: 'somestring',
3 b: 42
4};
5for (let [key, value] of Object.entries(object1)) {
6 console.log(`${key}: ${value}`);
7}
8// expected output:
9// "a: somestring"
10// "b: 42"
11// order is not guaranteed
1const object1 = {
2 a: 'somestring',
3 b: 42
4};
5
6for (let [key, value] of Object.entries(object1)) {
7 console.log(`${key}: ${value}`);
8}
9
10// expected output:
11// "a: somestring"
12// "b: 42"
13// order is not guaranteed
14
1const obj = {
2 a: "aa",
3 b: "bb",
4 c: "cc",
5};
6//This for loop will loop through all keys in the object.
7// You can get the value by calling the key on the object with "[]"
8for(let key in obj) {
9 console.log(key);
10 console.log(obj[key]);
11}
12
13//This will return the following:
14// a
15// aa
16// b
17// bb
18// c
19// cc
1var lunch = {
2 sandwich: 'ham',
3 snack: 'chips',
4 drink: 'soda',
5 desert: 'cookie',
6 guests: 3,
7 alcohol: false,
8};
9
10Object.keys(lunch).forEach(function (item) {
11 console.log(item); // key
12 console.log(lunch[item]); // value
13});
14
15// returns "sandwich", "ham", "snack", "chips", "drink", "soda", "desert", "cookie", "guests", 3, "alcohol", false
16