Skip to content

Commit 6d4aa84

Browse files
Merge pull request akshitagit#147 from akwe-afriitech/akwe-afriitech-patch-1
feat added variouse arrray iterators
2 parents b3be6ac + 19317cd commit 6d4aa84

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Array Methods/filter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
filter
2+
// does return a new array
3+
// can manipulate the size of new array
4+
// returns based on condition
5+
6+
const people = [
7+
{ name: 'bob', age: 20, position: 'developer' },
8+
{ name: 'peter', age: 25, position: 'designer' },
9+
{ name: 'susy', age: 30, position: 'the boss' },
10+
{ name: 'anna', age: 35, position: 'the boss' },
11+
];
12+
13+
const youngPeople = people.filter(function (person) {
14+
return person.age <= 25;
15+
});
16+
17+
const developers = people.filter(function (person) {
18+
return person.position === 'senior developer';
19+
});
20+
21+
console.log(developers);

Array Methods/forEach.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// forEach
2+
// does not return new array
3+
4+
const people = [
5+
{ name: 'bob', age: 20, position: 'developer' },
6+
{ name: 'peter', age: 25, position: 'designer' },
7+
{ name: 'susy', age: 30, position: 'the boss' },
8+
];
9+
10+
function showPerson(person) {
11+
console.log(person.position.toUpperCase());
12+
}
13+
14+
// people.forEach(showPerson);
15+
16+
people.forEach(function (item) {
17+
console.log(item.position.toUpperCase());
18+
});

Array Methods/map.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// map
2+
// does return a new array
3+
// does not change size of original array
4+
// uses values from original array when making new one
5+
6+
const people = [
7+
{ name: 'bob', age: 20, position: 'developer' },
8+
{ name: 'peter', age: 25, position: 'designer' },
9+
{ name: 'susy', age: 30, position: 'the boss' },
10+
{ name: 'anna', age: 35, position: 'the boss' },
11+
];
12+
13+
const ages = people.map(function (person) {
14+
return person.age + 20;
15+
});
16+
const newPeople = people.map(function (person) {
17+
return {
18+
firstName: person.name.toUpperCase(),
19+
oldAge: person.age + 20,
20+
};
21+
});
22+
23+
const names = people.map(function (person) {
24+
return `<h1>${person.name}</h1>`;
25+
});
26+
27+
document.body.innerHTML = names.join('');
28+
29+
console.log(names);

0 commit comments

Comments
 (0)