๐Ÿ“–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()

JavaScript 30 ํ”„๋กœ์ ํŠธ ๊ฒฐ๊ณผ๋ฌผ

'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