[JS] ๐๏ธJavaScript 30 - Day 4
๐Day 4 - Array Cardio Day 1
JavaScript 30์ Day 4 ํ๋ก์ ํธ๋ ๋ฌด์ธ๊ฐ ๋ง๋๋ ํ๋ก์ ํธ๋ ์๋๊ณ , ์๋ฐ์คํฌ๋ฆฝํธ์ Array์ ๋ํด์ ์์๋ณด๋ ์๊ฐ์ด๋ค. Array์ ๋ค์ํ method๋ค์ ๊ณต๋ถํด๋ณด์.
๐ค๐์ฝ๋ ๋ชจ์๋ณด๊ธฐ
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Array Cardio ๐ช</title>
</head>
<body>
<p><em>Psst: have a look at the JavaScript Console</em> ๐</p>
<script>
// Get your shorts on - this is an array workout!
// ## Array Cardio Day 1
// Some data we can work with
const inventors = [
{ first: "Albert", last: "Einstein", year: 1879, passed: 1955 },
{ first: "Isaac", last: "Newton", year: 1643, passed: 1727 },
{ first: "Galileo", last: "Galilei", year: 1564, passed: 1642 },
{ first: "Marie", last: "Curie", year: 1867, passed: 1934 },
{ first: "Johannes", last: "Kepler", year: 1571, passed: 1630 },
{ first: "Nicolaus", last: "Copernicus", year: 1473, passed: 1543 },
{ first: "Max", last: "Planck", year: 1858, passed: 1947 },
{ first: "Katherine", last: "Blodgett", year: 1898, passed: 1979 },
{ first: "Ada", last: "Lovelace", year: 1815, passed: 1852 },
{ first: "Sarah E.", last: "Goode", year: 1855, passed: 1905 },
{ first: "Lise", last: "Meitner", year: 1878, passed: 1968 },
{ first: "Hanna", last: "Hammarstrรถm", year: 1829, passed: 1909 },
];
const people = [
"Bernhard, Sandra",
"Bethea, Erin",
"Becker, Carl",
"Bentsen, Lloyd",
"Beckett, Samuel",
"Blake, William",
"Berger, Ric",
"Beddoes, Mick",
"Beethoven, Ludwig",
"Belloc, Hilaire",
"Begin, Menachem",
"Bellow, Saul",
"Benchley, Robert",
"Blair, Robert",
"Benenson, Peter",
"Benjamin, Walter",
"Berlin, Irving",
"Benn, Tony",
"Benson, Leana",
"Bent, Silas",
"Berle, Milton",
"Berry, Halle",
"Biko, Steve",
"Beck, Glenn",
"Bergman, Ingmar",
"Black, Elk",
"Berio, Luciano",
"Berne, Eric",
"Berra, Yogi",
"Berry, Wendell",
"Bevan, Aneurin",
"Ben-Gurion, David",
"Bevel, Ken",
"Biden, Joseph",
"Bennington, Chester",
"Bierce, Ambrose",
"Billings, Josh",
"Birrell, Augustine",
"Blair, Tony",
"Beecher, Henry",
"Biondo, Frank",
];
// Array.prototype.filter()
// 1. Filter the list of inventors for those who were born in the 1500's
const fifteen = inventors.filter(function (inventor) {
if (inventor.year >= 1500 && inventor.year < 1600) {
return true;
}
});
console.table(fifteen);
// Array.prototype.map()
// 2. Give us an array of the inventors first and last names
const fullNames = inventors.map(function (inventor) {
return `${inventor.first} ${inventor.last}`;
});
console.log(fullNames);
// Array.prototype.sort()
// 3. Sort the inventors by birthdate, oldest to youngest
// const ordered = inventors.sort(function (a, b) {
// if (a.year > b.year) {
// return 1;
// } else {
// return -1;
// }
// });
const ordered = inventors.sort((a, b) => (a.year > b.year ? 1 : -1));
console.table(ordered);
// Array.prototype.reduce()
// 4. How many years did all the inventors live all together?
const totalYears = inventors.reduce(function (total, inventor) {
return total + (inventor.passed - inventor.year);
}, 0);
console.log(totalYears);
// 5. Sort the inventors by years lived
const oldest = inventors.sort(function (a, b) {
const lastGuy = a.passed - a.yaer;
const nextGuy = b.passed - b.year;
// if (lastGuy > nextGuy) {
// return -1;
// } else {
// return 1;
// }
return lastGuy > nextGuy ? -1 : 1;
});
console.table(oldest);
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
// const category = document.querySelector(".mw-category");
// const links = [...category.querySelectorAll("a")];
// const de = links
// .map((link) => link.textContent)
// .filter((streetName) => streetName.includes("de"));
// 7. sort Exercise
// Sort the people alphabetically by last name
const alpha = people.sort(function (lastOne, nextOne) {
const [aLast, aFirst] = lastOne.split(", ");
const [bLast, bFirst] = nextOne.split(", ");
return aLast > bLast ? 1 : -1;
});
console.log(alpha);
// 8. Reduce Exercise
// Sum up the instances of each of these
const data = [
"car",
"car",
"truck",
"truck",
"bike",
"walk",
"car",
"van",
"bike",
"walk",
"car",
"van",
"car",
"truck",
];
const transportation = data.reduce(function (obj, item) {
if (!obj[item]) {
obj[item] = 0;
}
obj[item]++;
return obj;
}, {});
console.log(transportation);
</script>
</body>
</html>
๐์ฝ๋ ์ค๋ช
์ด๋ฒ ํ๋ก์ ํธ์์๋ Array์ ๋ค์ํ method๋ค์ ๊ณต๋ถํ๋ค.
Array.filter()
filter()
๋ฉ์๋๋ ๋ฐฐ์ด ๋ด ๊ฐ ์์์ ๋ํด ํ ๋ฒ ์ ๊ณต๋ callback ํจ์๋ฅผ ํธ์ถํด, callback์ด true๋ก ๊ฐ์ ํ๋ ๊ฐ์ ๋ฐํํ๋ ๋ชจ๋ ๊ฐ์ด ์๋ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๋ค. callback์ ํ ๋น๋ ๊ฐ์ด ์๋ ๋ฐฐ์ด์ ์ธ๋ฑ์ค์ ๋ํด์๋ง ํธ์ถ๋๋ค. ์ด ๋, ์ญ์ ๋๊ฑฐ๋ ๊ฐ์ด ํ ๋น๋ ์ ์ด ์๋ ์ธ๋ฑ์ค์ ๋ํด์๋ ํธ์ถ๋์ง ์๋๋ค. callback ํ
์คํธ๋ฅผ ํต๊ณผํ์ง ๋ชปํ ๋ฐฐ์ด ์์๋ ๊ทธ๋ฅ ๊ฑด๋๋ฐ๋ฉฐ ์๋ก์ด ๋ฐฐ์ด์ ํฌํจ๋์ง ์๋๋ค.
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
Array.map()
map()
๋ฉ์๋๋ callback ํจ์๋ฅผ ๊ฐ๊ฐ์ ์์์ ๋ํด ํ๋ฒ์ฉ ์์๋๋ก ๋ถ๋ฌ ๊ทธ ํจ์์ ๋ฐํ๊ฐ์ผ๋ก ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ ๋ค. callback ํจ์๋ (undefined๋ ํฌํจํด์) ๋ฐฐ์ด ๊ฐ์ด ๋ค์ด์๋ ์ธ๋ฑ์ค์ ๋ํด์๋ง ํธ์ถ๋๋ค. ์ฆ, ๊ฐ์ด ์ญ์ ๋๊ฑฐ๋ ์์ง ๊ฐ์ด ํ ๋น/์ ์๋์ง ์์ ์ธ๋ฑ์ค์ ๋ํด์๋ ํธ์ถ๋์ง ์๋๋ค.
const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
Array.sort([compareFunction])
sort()
๋ฉ์๋๋ compareFunction์ด ์ ๊ณต๋์ง ์์ผ๋ฉด ์์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๊ณ ์ ๋ ์ฝ๋ ์ฝ๋ ํฌ์ธํธ ์์๋ก ๋ฌธ์์ด์ ๋น๊ตํ์ฌ ์ ๋ ฌ๋๋ค. compareFunction์ด ์ ๊ณต๋๋ฉด ๋ฐฐ์ด ์์๋ compare ํจ์์ ๋ฐํ ๊ฐ์ ๋ฐ๋ผ ์ ๋ ฌ๋๋ค. a์ b๊ฐ ๋น๊ต๋๋ ๋ ์์๋ผ๋ฉด, ๋ค์๊ณผ ๊ฐ์ ๊ท์น์ ๋ฐ๋ฅธ๋ค.
- compareFunction(a, b)์ด 0๋ณด๋ค ์์ ๊ฒฝ์ฐ a๋ฅผ b๋ณด๋ค ๋ฎ์ ์ธ๋ฑ์ค๋ก ์ ๋ ฌํ๋ค.
- compareFunction(a, b)์ด 0๋ณด๋ค ํฐ ๊ฒฝ์ฐ, b๋ฅผ a๋ณด๋ค ๋ฎ์ ์ธ๋ฑ์ค๋ก ์ ๋ ฌํ๋ค.
- compareFunction(a, b)์ด 0์ ๋ฐํํ๋ฉด a์ b๋ฅผ ์๋ก์ ๋ํด ๋ณ๊ฒฝํ์ง ์๊ณ ๋ชจ๋ ๋ค๋ฅธ ์์์ ๋ํด ์ ๋ ฌํ๋ค.
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// expected output: Array [1, 2, 3, 4, 5]
Array.reduce(callback, [initialValue])
reduce()
๋ฉ์๋๋ ๋ ๋น ์์๋ฅผ ์ ์ธํ๊ณ ๋ฐฐ์ด ๋ด์ ์กด์ฌํ๋ ๊ฐ ์์์ ๋ํด callback ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํํ๊ณ , ํ๋์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋ค. initialValue๋ฅผ ์ ๊ณตํ์ง ์์ผ๋ฉด, reduce()๋ ์ธ๋ฑ์ค 1๋ถํฐ ์์ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๊ณ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ ๊ฑด๋ ๋ด๋ค. initialValue๋ฅผ ์ ๊ณตํ๋ฉด ์ธ๋ฑ์ค 0์์ ์์ํ๋ค.
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
})
previousValue | currentValue | currentIndex | array | return value |
---|---|---|---|---|
0 | 1 | 1 | [0, 1, 2, 3, 4] | 1 |
1 | 2 | 2 | [0, 1, 2, 3, 4] | 3 |
3 | 3 | 3 | [0, 1, 2, 3, 4] | 6 |
6 | 4 | 4 | [0, 1, 2, 3, 4] | 10 |
[0, 1, 2, 3, 4].reduce((previousValue, currentValue, currentIndex, array) => {
return previousValue + currentValue;
}, 10)
previousValue | currentValue | currentIndex | array | return value |
---|---|---|---|---|
10 | 0 | 0 | [0, 1, 2, 3, 4] | 10 |
10 | 1 | 1 | [0, 1, 2, 3, 4] | 11 |
11 | 2 | 2 | [0, 1, 2, 3, 4] | 13 |
13 | 3 | 3 | [0, 1, 2, 3, 4] | 16 |
16 | 4 | 4 | [0, 1, 2, 3, 4] | 20 |
๐TIL(Today I Learned)
- JavaScript Array์ ๋ค์ํ method๋ค์ ์์๋ณผ ์ ์์๋ค.
filter()
,map()
,sort()
,reduce()
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] ๐๏ธJavaScript 30 - Day 6 (0) | 2021.11.19 |
---|---|
[JS] ๐๏ธJavaScript 30 - Day 5 (0) | 2021.11.18 |
[JS] ๐๏ธJavaScript 30 - Day 3 (0) | 2021.11.12 |
[JS] ๐๏ธJavaScript 30 - Day 2 (0) | 2021.11.11 |
[JS] ๐๏ธJavaScript 30 - Day 1 (0) | 2021.11.10 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[JS] ๐๏ธJavaScript 30 - Day 6
[JS] ๐๏ธJavaScript 30 - Day 6
2021.11.19 -
[JS] ๐๏ธJavaScript 30 - Day 5
[JS] ๐๏ธJavaScript 30 - Day 5
2021.11.18 -
[JS] ๐๏ธJavaScript 30 - Day 3
[JS] ๐๏ธJavaScript 30 - Day 3
2021.11.12 -
[JS] ๐๏ธJavaScript 30 - Day 2
[JS] ๐๏ธJavaScript 30 - Day 2
2021.11.11