Upgrade to Pro — share decks privately, control downloads, hide ads and more …

改めて理解するJavaScript Array / Understanding JavaScr...

改めて理解するJavaScript Array / Understanding JavaScript Array

2018/5/19 初夏のJavaScript祭 https://javascript-fes.doorkeeper.jp/events/73314 にて発表したものです。

OKUNOKENTARO

May 19, 2018
Tweet

More Decks by OKUNOKENTARO

Other Decks in Technology

Transcript

  1. +BWB4DSJQUʹ͓͚Δ഑ྻ w ഑ྻ "SSBZ Λѻ͏ʹ͸
 const fruits = ['Apple', 'Banana'];

    w ഑ྻ͔Β஋ΛऔΓग़͢ʹ͸
 console.log(fruits[0]); // Apple
  2. "SSBZͷϝιο υ w +BWB4DSJQUͰͷ഑ྻͱ͸ɺ "SSBZΠϯελϯεͰ͋Δ w Array.prototype.xxxx()ͱ͍͏ ϝ ιο υΛ਺ଟ࣋ͪ͘


    [0, 1, 2].xxxx()ͷΑ ͏ʹͯ͠࢖͑Δ • https://developer.mozilla.org/ja/docs/Web/JavaScript/ Reference/Global_Objects/Array
  3. &4ʹ͸഑ྻૢ࡞͕๛෋ w &4ʹ͸ɺ ഑ྻૢ࡞ʹؔ͢Δϝ ιο υ͕ଟ͘௥Ճ͞Ε͍ͯΔ w #map(), #filter(), #forEach(),

    #some(), #every(), #reduce()ͳͲ w *&͕৺഑ ʁ 
 શϒϥ΢βରԠ ʢ೥ʹ͓͍ͯʣ
  4. ͨͱ͑͹NBQ ͸ • const arr = ['A', 'B', 'C']; •

    $.map(arr, function(v, i) {
 return v + i;
 }); • _.map(arr, function(v, i) {
 return v + i;
 }); • // ['A0', 'B1', 'C2']
  5. ඪ४Λ࢖͏ ͱ • const arr = ['A', 'B', 'C']; •

    arr.map(function(v, i) {
 return `${v}${i}`;
 }); • arr.map((v, i) => `${v}${i}`); • // ['A0', 'B1', 'C2']
  6. ͳͥϥΠ ϒϥ ϦΛ࢖Θͳ͍΄͏͕Α͍ͷ͔ w ґଘઌ͸গͳ͚Ε͹গͳ͍΄ͲΑ͍ w ࣗ෼ͷίʔ υ͸෗Δ͕ɺ ଞਓͷίʔ υ΋෗Δ

    w ґଘ͕ଟ͚Ε͹ଟ͍΄Ͳɺ ͦΕͧΕͷϥΠ ϒϥ ϦͷόʔδϣϯΛ
 ߋ৽͢Δख͕ؒൃੜ w ϝϯςφϯε͞ΕΔظؒ͸ɺ ϥΠ ϒϥ ϦΑ Γඪ४ͷ΄͏͕ ʢগͳ͘ ͱ΋ʣ ௕͍
  7. ύϑΥʔϚϯε্ͷ؍఺ w ඪ४͸ωΠςΟ ϒίʔ υͱͯ͠ಈ࡞ w ϥΠ ϒϥ Ϧ͸+BWB4DSJQUͱͯ͠ॻ͔Εɺ ϥϯλΠϜͰಈ࡞

    w ϥΠ ϒϥ ϦΛ࢖Θͳ͍͜ͱͰɺ μ΢ϯϩʔ υ ʢ$%/ܦ༝ʣ ͷ࣌ؒͷ୹ॖ΍
 8FCQBDLͰͷόϯ υϧ݁Ռͷ༰ྔ࡟ݮ
  8. Ͳ͏ ͯ͠΋࢖͍͍ͨͳΒ w ͦΕͰ΋MPEBTIͷ_.isEqual()͚ͩ͸࢖͍͍ͨΜͩ ʂ 
 Έ͍ͨͳέʔε w npm i

    --save lodash
 Ͱ͸ͳ͘
 npm i --save lodash.isequal
 ͱͯ͠ɺ ඞཁͳ΋ͷ͚ͩΠϯε τʔϧ͢Δͷ͕Α͍
  9. ഑ྻ಺͔Β֘౰ͷͻͱͭΛऔΓग़͢ w ࣮ࡍʹ͋ͬͨ࿩ w const fruits = ['Apple', 'Banana', 'Orange'];

    w Banana͚ͩऔΓग़͍ͨ͠
 ʢԿݸ໨ʹ͋Δ͔͸෼͔͍ͬͯͳ͍΋ͷͱ͢Δʣ
  10. ޡͬͨϝιο υબ୒͸ҙਤ͕఻ΘΓʹ͍͘ w fruits.filter(v => v === 'Banana')[0]; // Banana

    w #filter()ͯ͠[0]Λ͚ͭΔͱ͍͏΍ΓํͰ͸
 ʮϑΟϧλʔ͍ͨ͠ͷ͔ʯ ɺ ʮͻͱ͚ͭͩऔΓग़͍ͨ͠ͷ͔ʯ 
 ίʔ υΛಡΉਓʹҙਤ͕఻ΘΓʹ͍͘
  11. ҙਤͷ఻ΘΔϝιο υબ୒ w fruits.find(v => v === 'Banana'); // Banana

    w ͜ͷྫͰ͸#filter()͔ͯ͠Β[0]͢ΔͷͰ͸ͳ͘
 #find()ͱ͍͏ద੾ͳϝ ιο υ͕ଘࡏ͢Δ
  12. ίʔ υͱ͸ҙਤΛ఻͑Δ΋ͷ w ద੾ͳϝ ιο υͷ࢖༻Ͱɺ ॻ͖खͷҙਤΛద੾ʹಡΈखʹ఻͑Δ w ಈ͚͹͍͍΍Ͱ͸ͳ͍ w

    ಈ͔͢͜ͱΑ Γɺ ఻͑Δ͜ͱ͕ང͔ʹॏཁ w ඪ४"1*ʹͲΜͳϝ ιο υ͕͋Γɺ Ͳ͏͍͏࣌ʹ࢖͑͹࠷ద͔
 ͍͍ͩͨ͸͓͍֮͑ͯͨ΄͏͕Α͍ ʢ͢΂ͯΛ҉ه͢Δඞཁ͸ͳ͍ʣ
  13. GPSจ͸ෆཁͳͷ͔ w ࢖Θͳ͍͜ͱ͸૿͕͑ͨɺ ෆཁͰ͸ͳ͍ w ͨͩ͠ ʮύϑ ΥʔϚϯεͷͨΊʹGPSจΛ࢖͑͹Α͍ʯ ͱ͍͏ೝࣝ͸
 ΋͸΍աڈͷ΋ͷ

    w ్தͰϧʔϓΛൈ͚͍ͨ৔߹"SSBZͷϝ ιο υͰ͸ແཧͳͷͰ༗༻ w ࣮ࡍ͸GPSจΑ ΓGPSPGจͷํָ͕