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});
1var person={
2 first_name:"johnny",
3 last_name: "johnson",
4 phone:"703-3424-1111"
5};
6for (var property in person) {
7 console.log(property,":",person[property]);
8}
1const object1 = {
2 a: 'somestring',
3 b: 42,
4 c: false
5};
6
7console.log(Object.values(object1));
8// expected output: Array ["somestring", 42, false]
1const object1 = {
2 a: 'somestring',
3 b: 42
4};
5
6for (const [key, value] of Object.entries(object1)) {
7 console.log(`${key}: ${value}`);
8}
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