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
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}
1var sandwiches = [
2 'tuna',
3 'ham',
4 'turkey',
5 'pb&j'
6];
7
8sandwiches.forEach(function (sandwich, index) {
9 console.log(index);
10 console.log(sandwich);
11});
12
13// returns 0, "tuna", 1, "ham", 2, "turkey", 3, "pb&j"
14
1var people=[
2 {first_name:"john",last_name:"doe"},
3 {first_name:"mary",last_name:"beth"}
4];
5for (let i = 0; i < people.length; i++) {
6 console.log(people[i].first_name);
7}
1var arr = [{id: 1},{id: 2},{id: 3}];
2
3for (var elm of arr) {
4 console.log(elm);
5}
1var array = ["e", 5, "cool", 100];
2
3for (let i = 0; i < array.length; i++) {
4 console.log(array[i]);
5}
6
7// This is a common method used to loop through elements in arrays.
8// You can use this to change elements, read them, and edit them