1var p = {
2 "p1": "value1",
3 "p2": "value2",
4 "p3": "value3"
5};
6
7// for-in
8for (var key in p) {
9 if (p.hasOwnProperty(key)) {
10 console.log(key + " -> " + p[key]);
11 }
12}
13
14// for-of with Object.keys()
15for (var key of Object.keys(p)) {
16 console.log(key + " -> " + p[key])
17}
18
19// Object.entries()
20for (let [key, value] of Object.entries(p)) {
21 console.log(`${key}: ${value}`);
22}
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 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}
1// Create a new array based on the original but without modifying it
2
3const myArray = [{x:100}, {x:200}, {x:300}];
4
5const newArray= myArray.map(element => {
6 return {
7 ...element,
8 x: element.x * 2
9 };
10});
11
12console.log(myArray); // [100, 200, 300]
13console.log(newArray); // [200, 400, 600]
14
1for (let key in yourobject) {
2 if (yourobject.hasOwnProperty(key)) {
3 console.log(key, yourobject[key]);
4 }
5}
6