1/* new options with IE6: loop through array of objects */
2
3const people = [
4 {id: 100, name: 'Vikash'},
5 {id: 101, name: 'Sugam'},
6 {id: 102, name: 'Ashish'}
7];
8
9// using for of
10for (let persone of people) {
11 console.log(persone.id + ': ' + persone.name);
12}
13
14// using forEach(...)
15people.forEach(person => {
16 console.log(persone.id + ': ' + persone.name);
17});
18// output of above two methods
19// 100: Vikash
20// 101: Sugam
21// 102: Ashish
22
23
24// forEach(...) with index
25people.forEach((person, index) => {
26 console.log(index + ': ' + persone.name);
27});
28// output of above code in console
29// 0: Vikash
30// 1: Sugam
31// 2: Ashish
32
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 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}
1let arr = [object0, object1, object2];
2
3for (let elm of arr) {
4 console.log(elm);
5}
1var myArr = [{name: 'rich', secondName: 'james'}, {name: 'brian', secondName: 'chris'}];
2
3var mySecondArr = myArr.map(x => x.name);
4console.log(mySecondArr);
1// Array of objects
2const p = [{
3 "p1": "value1",
4 "p2": "value2",
5 "p3": "value3"
6},
7{
8 "p4": "value4",
9 "p5": "value5",
10 "p6": "value6"
11}];
12
13// Get the objects out of the array
14for (let obj of p) {
15 // console.log(obj);
16 // output:
17 // { p1: 'value1', p2: 'value2', p3: 'value3' }
18 // { p4: 'value4', p5: 'value5', p6: 'value6' }
19 // Now we can loop the objects in the array by nesting the 'for in' loop inside the 'for of' loop
20 for(let key in obj) {
21 console.log(key);
22 // output:
23 // p1
24 // p2
25 // p3
26 // p4
27 // p5
28 // p6
29 // console.log(obj[key]);
30 // output
31 // value1
32 // value2
33 // value3
34 // value4
35 // value5
36 // value6
37 }
38}