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}
1const obj = { a: 1, b: 2 };
2
3Object.keys(obj).forEach(key => {
4 console.log("key: ", key);
5 console.log("Value: ", obj[key]);
6} );
1var obj = { foo: 'bar', baz: 42 };
2
3Object.keys(obj).forEach((key) => {
4 const el = obj[key];
5 console.log(
6 {
7 key: key,
8 value: el
9 }
10 );
11});
1var obj = { foo: 'bar', baz: 42 };
2console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
3
4for(const [i, val] of Object.entries(obj))
5 new_obj[i] = val;
1const obj = { 0: 'a', 1: 'b', 2: 'c' };
2console.log(Object.entries(obj)); // => [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
3
4Object.entries(obj).forEach((key, value) => {
5 console.log(key + ' ' + value);
6});
1var person = {"name":"taylor","age":31};
2for (property in person) {
3 console.log(property,person[property]);
4}
5//name taylor
6//age 31