- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35_array_foreach.js
More file actions
Latest commit
30 lines (25 loc) · 782 Bytes
/
Copy path35_array_foreach.js
File metadata and controls
30 lines (25 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// for each
// doesn't return new array
// method executes a provided function once for each array element
constpeople=[
{name: 'bobo',born: 1987,position: 'developer'},
{name: 'peter',born: 1989,position: 'designer'},
{name: 'sussy',born: 1975,position: 'the boss'},
];
// remember this method not return 3 new array
// let coy = people.forEach(function () {});
// console.log(coy); //undefined
// the way 1
// let coy = function (person) {
// console.log(person.name);
// };
// people.forEach(coy);
// the way 2
// u can callback function with directly use annonymous function
people.forEach(function(item){
lettotal=2020;
total-=item.born;
console.log(
`${item.name} with position ${item.position} now is ${total} years old`
);
});