11 Collection - iruma-tea/dokushujs GitHub Wiki
ã³ã¬ã¯ã·ã§ã³ ãšã¯ãå€ã®ãŸãšãŸãã§ç®¡çãããªããžã§ã¯ãã®äºã§ãã
JavaScriptã§äœ¿ããã³ã¬ã¯ã·ã§ã³ã¯ãES5ãŸã§ã¯ObjectãšArrayã®ã¿ã§ãããã
ES6ããMap,Set,WeakMap,WeakSetã䜿ããããã«ãªã£ãã
é
åã®å Žåã¯ãå€ã®ã¿ãæ ŒçŽ ãããæ ŒçŽãããå€ã¯ãããããå€ã«å¯Ÿå¿ãã
æ·»ãå(ã€ã³ããã¯ã¹) ã0ããé ã«æ¯ãããã
é åã¯ããªãã©ã«è¡šèšãŸãã¯ãArrayã䜿ã£ãŠåæåã§ããã
é åãªãã©ã«([])ãèšè¿°ããããšã§ãé åãåæåã§ããã
[æ§æ] ãªãã©ã«ã䜿ã£ãé
åã®åæå
const é
å = [å€1, å€2, å€3, ....];
(â») []ã¯ãé åã®ãªãã©ã«ãå®çŸ©ããããã®èšå·ã§ããçç¥å¯èœãè¡šã[]ã§ã¯ãªãã®ã§æ³šæã
ãªãã©ã«ã§é
åãåæåããäŸ
const fruits = ["banana", "orange", "grape"];
console.log(fruits);
new Arrayãèšè¿°ããŠé åãåæåããã
[æ§æ] Arrayã䜿çšããé
åã®åæå
ã»åŒæ°ã1ã€ã®å ŽåïŒ
const é
å = new Array(é
åã®é·ã);
ã»åŒæ°ã2ã€ä»¥äžã®å ŽåïŒ
const é
å = new Array(å€1, å€2,....);
new Arrayã§é
åãåæåããäŸ
const emptyArray = new Array(3);
console.log(emptyArray);
const filledArray = new Array(1, 2, 3);
console.log(filledArray);
åæåããé åã®æäœæ¹æ³ã«ã€ããŠãçæããé åã®ç¹å®ã®å€ãååŸã»å€æŽãããå Žåã¯ãæ·»åã䜿ãã
é
åã®èŠçŽ ã®ååŸã»å€æŽã®äŸ
const fruits = ["banana", "orange", "grape"];
console.log(fruits[0]);
> banana
fruits[1] = "apple";
console.log(fruits);
> ["banana", "apple", "grape"];
é
åã®èŠçŽ ã¯ã0çªç®ããå§ããç¹ã«æ³šæããŠãã ããã
ãŸããé
åã®é·ã(åæ°)ã¯ãlengthããããã£ããååŸã§ããã
é åã«ã¯ãæ§ã ãªã¡ãœãããçšæãããŠãããã¡ãœããã䜿ãããšã§ã宣èšããåŸã§ã§ããå€ã®ååŸã»å€æŽã»åé€ãªã©ãè¡ãããšãã§ããã
push - æ«å°Ÿã«è¿œå
pushã¡ãœããã¯ãé
åã®æ«å°Ÿã«èŠçŽ ãè¿œå ããŸãã
[æ§æ] pushã®èšæ³
é
å.push(è¿œå ãããèŠçŽ 1,[è¿œå ãããèŠçŽ 2,....])
const fruits = ["banana", "orange", "grape"];
fruits.push("apple");
consol.log(fruits);
> ["banana", "orange", "grape", "apple"]
unshit - å
é ã«è¿œå
unshitã¡ãœããã¯ãé
åã®å
é ã«èŠçŽ ãè¿œå ãã
[æ§æ] unshiftã®èšæ³
é
å.unshift(è¿œå ãããèŠçŽ 1,[è¿œå ãããèŠçŽ 2,....])
const fruits = ["banana", "orange", "grape"];
fruits.unshift("lemon");
console.log(fruits);
> ["lemon", "banana", "orange", "grape"];
é åããèŠçŽ ãåé€ãããšãã«äœ¿ãã¡ãœããã§ãã
shift - å é ãåé€ shiftã¡ãœããã¯ãé åã®å é ã®èŠçŽ ãåé€ãã
[æ§æ] shiftã®èšæ³
let åé€ããèŠçŽ = é
å.shift();
(䜿çšäŸ)
const fruits = ["banana", "orange", "grape"];
let returnVal = fruits.shift();
console.log(fruits);
> ["orange", "banana"]
console.log(returnVal);
> "banana"
pop - æ«å°Ÿãåé€ popã¡ãœããã¯ãé åã®æ«å°Ÿã®èŠçŽ ãåé€ããŸãã
[æ§æ]popã®èšæ³
let åé€ããèŠçŽ = é
å.pop();
(䜿çšäŸ)
const fruits = ["banana", "orange", "grape"];
let returnVal = fruits.pop();
> ["banana", "orange"]
console.log(returnVal);
> "grape"
splice - èŠçŽ ã®åãåã spliceã¡ãœããã¯ãéå§äœçœ®ããç¹å®ã®é·ãåã®èŠçŽ ãåãåãããã
[æ§æ] spliceã®èšæ³
let åãåãããé
å = å
ã®é
å.splice(éå§äœçœ®, é·ã[,èŠçŽ è¿œå 1,èŠçŽ è¿œå 2,....]);
(èŠçŽ ã®åãåãã®äŸ)
const fruits = ["banana", "orange", "grape"];
let deletedVal = fruits.splice(1, 2);
console.log(fruits)
> ["banana"]
console.log(deletedVal);
> ["orange", "grape"]
(èŠçŽ ã®åãåã£ãç®æã«èŠçŽ ãè¿œå )
const fruits = ["banana", "orange", "grape"];
fruits.splice(1, 1, "apple", "lemon");
é åã®çµåãè€è£œ(ã³ããŒ)ãè¡ãå Žåã«ã¯ãconcatã¡ãœããã䜿ãããŸããç¹å®ã®èŠçŽ ãè€è£œãããå Žåã«ã¯sliceã¡ãœãã䜿ãã
concat - é åã®çµå 以äžã®ããã«ãconcatã¡ãœããã䜿ããšãå¥ã®é åãçµåããæ°ããé åãäœæãã
[æ§æ]concatã®èšæ³(é
åã®çµå)
let çµåãããé
å = é
å1.concat(é
å2);
(䜿çšäŸ)
const fruits = ["banana", "orange", "grape"];
const newFruits = fruits.concat(["melon", "mango"]);
console.log(newFruits);
> ["banana", "orange", "grape", "melon", "mango"]
console.log(frutis);
> ["banana", "orange", "grape"]
[æ§æ]concatã®èšæ³(é
åã®è€è£œ)
let é
åãè€è£œããé
å = é
å.concat();
const fruits = ["banana", "orange", "grape"];
const newFruits = fruits.concat();
consle.log(newFruits);
> ["banana", "orange", "grape"]
slice - ç¹å®ã®èŠçŽ ãæœåºããæ°ããé
åãäœæ
sliceã¡ãœããã¯ãéå§äœçœ®ããçµäºäœçœ®ã®çŽåã®èŠçŽ ãŸã§ãæœåºããæ°ããé
åãè¿ãã
spliceãšéããå
ã®é
åã«å¯ŸããŠã¯åœ±é¿ãåãŒããŸããã
[æ§æ]sliceã®èšæ³
let åãåãããé
å = å
ã®é
å.slice([éå§äœçœ®,çµäºäœçœ®]);
const fruits = ["banana", "orange", "grape"];
const newFruits = fruits.slice(1, 2);
console.log(newFruits);
> ["orange"]
console.log(fruits);
> ["banana", "orange", "grape"]
const toEndFruits = frutis.slice(1);
console.log(toEndFruits);
> ["orange", "grape"]
const allFruits = fruits.slice();
console.log(allFruits);
> ["banana", "orange", "grape"]
indexOf - æåã«äžèŽããæ·»ãåãè¿ãã
indexOf ã¡ãœããã¯ãé
åå
ã§æåã«äžèŽããèŠçŽ ã®æ·»ãåãè¿ãã
[æ§æ] indexOfã®èšæ³
let äžèŽããæ·»ãå = é
å.indexOf(ååšç¢ºèªãããå€);
const fruits = ["banana", "orange", "grape"];
const found = fruits.indexOf("grape");
console.log(found);
> 2
const notFound = fruits.indexOf("watermelon");
console.log(notFound);
> -1
includes - å€ãååšããã確èª
includes ã¡ãœããã¯ãé
åå
ã®èŠçŽ ã«å€ãååšãããã確èªããã
[æ§æ] includesã®èšæ³
let çåœå€ = é
å.includes(ååšç¢ºèªãããå€);
const fruits = ["banana", "orange", "grape"];
const found = fruits.includes("grape");
console.log(found);
> true
const notFound = fruits.includes("lemon");
> false
join - èŠçŽ ãçµåããæååãäœæ
join ã¡ãœããã¯ãé
åã®èŠçŽ ãçµåããŠæååãäœæããã
[æ§æ] joinã®èšæ³
let çµåãããæåå = é
å.join([åºåãæå]);
åºåãæå: èŠçŽ ãçµåããéã®åºåãæåãèšå®ãããçç¥ãããšãã¯ã«ã³ã(,)ã§çµåãããã
(䜿çšäŸ)
const arry = ["Hello", "World"];
console.log(array.join(" "));
> Hello World
fill - èŠçŽ ãç¹å®ã®å€ã§åãã
fill ã¡ãœããã¯ãé
åã®éå§äœçœ®ãçµäºäœçœ®ã§ç¯å²æå®ããæ·»ãåã®èŠçŽ ã«å¯ŸããŠã
ç¹å®ã®å€ãèšå®ãããéå§äœçœ®ãçµäºäœçœ®ãçç¥ããå Žåããã¹ãŠã®èŠçŽ ãæå®ããå€ã§äžæžãããã
[æ§æ] fillã®èšæ³
é
å.fill(åèŠçŽ ã«èšå®ãããå€[,éå§äœçœ®,çµäºäœçœ®]);
(䜿çšäŸ)
const arry = new Array(100);
arry.fill(0);
console.log(arry);
> [0,0,0,0,...]
falt - å€æ¬¡å
é
åã®å¹³åŠå(次å
ãæžãã)
flat ã¡ãœããã¯ãå€æ¬¡å
é
åãåŒæ°(å¹³åŠåã¬ãã«)ã§æå®ãã次å
åãé
åã®æ§é ãå¹³åŠåããã
åŒæ°ãæå®ããªããšå Žåã«ã¯ã1次å
åãå¹³åŠåãããå€æ¬¡å
é
åãšã¯ãé
åæ§é ãå€éå±€ã§åœ¢æãããŠããé
åã®ããšã§ãã
é
åã®æ¬¡å
ã調æŽãããšãã«äœ¿ãã
[æ§æ] flatã®èšæ³
let å¹³åŠåãããé
å = å€æ¬¡å
é
å.flat([å¹³åŠåã¬ãã«])
(䜿çšäŸ)
const arry3D = [
1, [2], 3,
[4, [5]]
];
const arry2D = arry3D.flat();
console.log(array2D);
> [1, 2, 3, 4, [5]]
const arry1D = arry3D.flat(2);
console.log(arry1D);
> [1, 2, 3, 4, 5]
reverse - èŠçŽ ã®é çªãéã«çœ®æ
reverse ã¡ãœããã¯ãé
åã®èŠçŽ ã®äžŠã³ãéã«ãã
[æ§æ] reverseã®èšæ³
é
å.reverse();
(䜿çšäŸ)
const arry = [1,2,3,4,5]
arry.reverse();
console.log(arry);
> [5, 4, 3, 2, 1]
é
åãä¿æããã¡ãœããã«ã¯ãã³ãŒã«ããã¯é¢æ°ãåŒæ°ã«åããã®ãããã
ã³ãŒã«ããã¯é¢æ°ãåŒæ°ã«åãã¡ãœããã¯ãé
åã®èŠçŽ ã«å¯Ÿããç¹°ãè¿ãåŠçã®èšè¿°ã®ç°¡ç¥åãåŠçã®å
容ãæ確ããç®çã§äœ¿ãããã
forEachã¡ãœããã䜿ãããšã§ãé åã®èŠçŽ ã«å¯Ÿããç¹°ãè¿ãåŠçãã³ãŒã«ããã¯é¢æ°ãå©çšããŠèšè¿°ã§ããã
[æ§æ] forEachã®èšæ³
é
å.forEach(function(value, index, arry) {
/* é
åã®åèŠçŽ ã䜿ã£ãåŠç */
}[,_this]);
value: é
åã®å€ã1ã€ãã€æž¡ãããŠããã
index: é
åã®æ·»ãåã1ã€ãã€æž¡ãããŠããã
arry: é
åèªäœãæž¡ãããã
_this: ã³ãŒã«ããã¯é¢æ°å
ã®thisã®åç
§å
ãèšå®ããã
(䜿çšäŸ)
const arry = [1, 2, 3, 4, 5];
arry.forEach(function(value, index, arry) {
console.log(value, index, arry);
});
> 1 0 [1, 2, 3, 4, 5]
> 2 1 [1, 2, 3, 4, 5]
> 3 2 [1, 2, 3, 4, 5]
> 4 3 [1, 2, 3, 4, 5]
> 5 4 [1, 2, 3, 4, 5]
(䜿çšäŸ ã¢ããŒé¢æ°)
const arry = [1, 2, 3, 4, 5];
arry.forEach((value, index, arry) => console.log(value, index, arry));
> 1 0 [1, 2, 3, 4, 5]
> 2 1 [1, 2, 3, 4, 5]
> 3 2 [1, 2, 3, 4, 5]
> 4 3 [1, 2, 3, 4, 5]
> 5 4 [1, 2, 3, 4, 5]
(䜿çšäŸ ååä»ãé¢æ°)
const arry = [1, 2, 3, 4, 5];
function multiply5(value) {
console.log(value * 5);
}
arry.forEarch(multiply5);
> 5
> 10
> 15
> 20
> 25
map ã¡ãœããã¯ãã³ãŒã«ããã¯é¢æ°ã®æ»ãå€ãèŠçŽ ã«åãæ°ããé åãè¿ãã
[æ§æ] mapã®èšæ³
let æ°ããé
å = é
å.map((value, index, arry) => {
return æ°ããé
åã«è¿œå ãããå€;
}, [,this]);
mapã¡ãœããã®äœ¿ãæ¹ã¯ãforEachã¡ãœãããšåãã§ããããmapã¯æ»ãå€ãšããŠæ°ããé åãè¿ãç¹ãããšãªãã
(䜿çšäŸ) å
ã®é
åã®æ°å€ã«5ãæããå€ãä¿æããé
åãäœæ
const arry = [1, 2, 3, 4, 5];
function multiply5(value) {
return value * 5;
}
const newArry = arry.map(multiply5);
// const newArry = arry.map(value => value * 5); ãšåãæå³
console.log(newArry);
> [5, 10, 15, 20, 25]
filterã¡ãœããã¯ãã³ãŒã«ããã¯é¢æ°ã®æ¡ä»¶ãtrueã«ãªã£ããšãã®é åã®èŠçŽ ã®ã¿ãä¿æããæ°ããé åãäœæããã
[æ§æ] filterã®èšæ³
let æ°ããé
å = é
å.filter((value, index, arry) => {
return çåœå€; // trueã®ãšããvalueãæ°ããé
åã«è¿œå ããã
},[_this]);
(䜿çšäŸ)
const arry = [1, 2, 3, 4, 5];
function gt3(value) {
return value > 3;
}
const newArry = arry.filter(gt3);
console.log(newArry);
> [4, 5]
everyã¡ãœããã¯ããã¹ãŠã®èŠçŽ ãã³ãŒã«ããã¯é¢æ°ã§å®è£
ãããæ¡ä»¶ã§trueãšãªããã©ããããã¹ãããã
ãã¹ãŠã®æ¡ä»¶ãäžèŽããå Žåã«çµæãtrueãšãªãããã以å€ã¯falseã«ãªãã
[æ§æ]everyã®èšæ³
let çµæ = é
å.every((value, index, arry) => {
return çåœå€;
},[_this]);
(䜿çšäŸ)
const arry = [1, 2, 3, 4, 5];
const result = arry.every(value => value > 0);
console.log(result);
> true
const result2 = arry.every(value => value > 3);
console.log(result2);
> false
someã¡ãœããã¯ãå°ãªããšã1ã€ã®å€ãã³ãŒã«ããã¯é¢æ°ã§å®è£
ãããæ¡ä»¶ã§trueãšãªããã©ããããã¹ãããã
1ã€ãæ¡ä»¶ã«äžèŽããªãå Žåã«ã¯çµæãfalseã«ãªãããã以å€ã¯trueã«ãªãã
[æ§æ] someã®èšæ³
let çµæ = é
å.some((value, index, arry) => {
return çåœå€;
}[,_this]);
(䜿çšäŸ)
const arry = [1, 2, 3, 4, 5];
const result1 = arry.some(value => value > 4);
console.log(result1);
> true
findã¡ãœããã¯ãã³ãŒã«ããã¯é¢æ°ã®æ¡ä»¶ãæºãããæåã®å€ãè¿ãã
[æ§æ] findã®èšæ³
let æ¡ä»¶ãæºããæåã®å€ = é
å.find((value, index, arry) => {
return çåœå€;
},[_this]);
(䜿çšäŸ)
const arry = [1, 2, 7, 8, 9];
const result = arry.find(value => value > 2);
console.log(result);
> 7
sort ã¡ãœããã¯ãèŠçŽ ã®äžŠã¹æ¿ããããšãã§ããã
[æ§æ] sortã®èšæ³
ã»æååãšããŠæé ã§èŠçŽ ã䞊ã¹æ¿ã
é
å.sort();
ã»æ¯èŒé¢æ°ã䜿ã£ãŠèŠçŽ ã䞊ã¹æ¿ã
é
å.sort(æ¯èŒé¢æ°);
(䜿çšäŸ-æé ã«ããå Žå)
const strArry = ["b", "c", "a"];
strArry.sort();
console.log(strArry);
> ["a", "b", "c"]
(䜿çšäŸ-æ¯èŒé¢æ°ã䜿ã£ã䞊ã¹æ¿ã)
function compare(val1, val2) {
if (val1 < val2) {
return -1; // æ»ãå€ã0ããå°ããå Žåãval1ãval2ã®åã«é
眮
}
if (val1 > val2) {
return 1; // æ»ãå€ã0ãã倧ããå Žåãval2ãval1ã®åã«é
眮
}
return 0; // æ»ãå€ã0ã®å Žåãval1, val2ã®é çªããã®ãŸãŸç¶æ
}
const arry = [10, 2, 7, 3, 9];
arry.sort(compare);
console.log(arry);
> [2, 3, 7, 9, 10]
æ¯èŒé¢æ°ã®æ»ãå€ãšäžŠã³é
æ»ãå€ | 䞊ã³é |
---|---|
è² ã®æ | val1 val2 |
æ£ã®æ | val2 val1 |
0ã®æ | 䞊ã³é ã¯å€æŽãªã |
reduceã¡ãœããã¯ãé åã®ã«ãŒãããŠåèŠçŽ ã®å€ããåäžã®åºåå€ãçæããŸãã
[æ§æ] reduceã®èšæ³
let result = é
å.reduce((totalValue, currentValue, index, arry) => {
return nextTotalValue;
},[,initValue])
totalValue: åã®ã«ãŒãã®æ»ãå€(nextTotalValue)ãæž¡ããããååã«ãŒãã®ãšãã«ã¯initValueã®å€(initValueãèšå®ãããŠããå Žå)ããŸãã¯é
åã®
1ã€ãç®ã®èŠçŽ ãæž¡ãããã
currentValue: é
åã®èŠçŽ ã1ã€ãã€æž¡ãããŠãããååã«ãŒãã®ãšãã¯é
åã®1ã€ç®ã®èŠçŽ ã®å€(initValueãèšå®ãããŠããå Žå)ããŸãã¯é
åã®2ã€ç®ã®èŠçŽ ã®å€ãæž¡ãããã
index: é
åã®æ·»ãåã1ã€ãã€æž¡ãããã
arry: é
åèªäœãæž¡ãããã
nextTotalValue: 次ã®ã«ãŒãã®totalValueã«èšå®ãããå€ãèšå®ããããŸããæåŸã®ã«ãŒãã®æ»ãå€ãreduceã¡ãœããã®çµæ(result)ãšãªãã
result: æåŸã®ã«ãŒãã®å€ãšãªãã
initValue: èšå®ãããå Žåã1ã«ãŒãç®ã®totalValueã«æž¡ãããã
(䜿çšäŸ)
function reducer(totalValue, currentValue) {
return totalValue + currentValue;
}
const arry = [1, 2, 3, 4, 5];
const result = arry.reduce(reducer);
console.log(result);
> 15;
Arrayã³ã³ã¹ãã©ã¯ã¿ã«ããéçã¡ãœããããããããçšæãããŠããã
from ã¡ãœããã䜿çšãããšãSetãªã©ã®å埩ãªããžã§ã¯ããargumentsãªã©ã®é å颚(array-like)ãªããžã§ã¯ãããé åãäœæã§ããã
ã»Setããé
åãäœæ
const set = new Set();
set.add(1);
set.add(2);
set.add(3);
const convertedArray = Array.from(set);
console.log(convertedArray);
> [1, 2, 3]
é åãã©ããã調ã¹ãã«ã¯ãisArray ã¡ãœããã䜿ãã
const set = new Set();
console.log(Array.isArray(set));
> false
const converedArray = Array.from(set);
console.log(Array.isArray(converedArray));
> true
åå²ä»£å ¥ããšã¯ é åããªããžã§ã¯ãã®èŠçŽ ãçŽæ¥ãå€æ°ãšããŠåãåºããŠäœ¿çšããèšæ³ã§ãã
é
åã®åå²ä»£å
¥ã§ã¯ãå³ãªãã©ã³ãã®é
åã®èŠçŽ ãå·Šãªãã©ã³ãã®å€æ°(å€æ°A,å€æ°B)ã«ããããã代å
¥ãããã
巊蟺ã®å€æ°ã¯ãéåžžã®å€æ°å®£èšãè¡ã£ãå€æ°ãšåãããã«äœ¿ãããšãã§ããã
[æ§æ] åå²ä»£å
¥ã®èšæ³(é
å)
let [å€æ°A, å€æ°B = åæå€B] = é
å;
(䜿çšäŸ)
let arry = [1, 2, 3];
let [x,y,z] = arry;
console.log(x, y, z);
> 1 2 3
let [x1, z1] = arry;
console.log(x1, z1);
> 1, 3
let x2;
[x2] = arry;
console.log(x2);
> 1
let [,,,a3 = 4] = arry;
console.log(a3);
> 4
let [x4, ...rest] = arry;
console.log(x4, rest);
> 1 [2, 3]
ãªããžã§ã¯ãã®åå²ä»£å ¥ã§ã¯ããããã£åãšå€æ°åãäžèŽããã å¿ èŠãããã
[æ§æ] åå²ä»£å
¥ã®èšæ³(ãªããžã§ã¯ã)
let {ããããã£åA, ããããã£åB = åæå€B} = ãªããžã§ã¯ã;
(äŸ)
let {banana, orange, apple} = { apple: "ãªã³ãŽ", banana: "ããã", orange: "ãªã¬ã³ãž"};
console.log(banana);
> ããã
let {banana: b, apple: a} = {apple: "ãªã³ãŽ", banana: "ããã"};
console.log(b, a);
> ããã ãªã³ãŽ
let {banana: b2="ããããããã"} = {apple: "ãªã³ãŽ"};
console.log(b2);
> ããããããã
let {banana: b3, ...fruits} = {apple: "ãªã³ãŽ", banana: "ããã", orange: "ãªã¬ã³ãž"};
console.log(fruits);
> {apple: "ãªã³ãŽ", orange: "ãªã¬ã³ãž"}
let { fruits: {apple: a3}} = { fruits: {apple: "ãªã³ãŽ", banana: "ããã"}};
console.log(a3);
> ãªã³ãŽ
let prop = "apple";
let {[prop]: a4} = {apple: "ãªã³ãŽ", banana: "ããã", orange: "ãªã¬ã³ãž"};
console.log(a4);
> ãªã³ãŽ
åå²ä»£å ¥ã¯ãé¢æ°ã®åŒæ°ãåãåããšãã«ã䜿çšå¯èœãé¢æ°ã®åŒæ°ã«åå²ä»£å ¥ã䜿ããšãæž¡ããããªããžã§ã¯ããé åãã ç¹å®ã®èŠçŽ ãæœåºããåŒæ°ã«æž¡ãããšãã§ããã
const fruitsArry = ["banana", "orange", "grape"];
const fruitsObj = {banana: "ããã", orange: "ãªã¬ã³ãž"};
function bunkatuArry([,,fruit3]) { // äžã€ç®ã®èŠçŽ ã®ã¿é¢æ°å
ã§äœ¿çšãã
console.log(fruit3);
}
bunkatuArry(fruitsArry);
> grape
function bunkatuObj({orange}) { // orangeã®ã¿åŒæ°ãšããŠæœåº
console.log(orange);
}
bunkatuObj(fruitsObj);
> ãªã¬ã³ãž
ES6ã§è¿œå ãããSetã¯ãäžæã®å€ãæ ŒçŽããã³ã¬ã¯ã·ã§ã³ãSetã«ã¯ãéè€ããå€ãä¿æããããšãã§ããªãã
Setã¯æ¬¡ã®ããã«åæåã§ããã
[æ§æ] Setã®åæå
ã»ç©ºã®Setãªããžã§ã¯ããäœæããå Žå
const Setãªããžã§ã¯ã = new Set();
ã»å埩å¯èœãªããžã§ã¯ãããSetãªããžã§ã¯ããäœæããå Žå
const Setãªããžã§ã¯ã = new Set(å埩å¯èœãªããžã§ã¯ã);
(äŸ) é
åããSetãªããžã§ã¯ããäœæ
const convertedSet = new Set([1, 2, 3]);
console.log(convertedSet);
> Set {1, 2, 3}
Setã«ã¯æ¬¡ã®ãããªã¡ãœãããããã
add ã¡ãœããã䜿ããšãSetãªããžã§ã¯ãã«å¯ŸããŠãå€ãè¿œå ããããšãã§ããã
ããªããã£ãå€ããªããžã§ã¯ãã«é¢ããããã©ã®ãããªå€ã§ãè¿œå å¯èœã
const fruits = new Set();
fruits.add("apple");
fruits.add("orange");
fruits.add("orange"); // éè€ããå€ãè¿œå
console.log(fruits);
> Set {"apple", "orange"}
delete ã¡ãœããã䜿çšãããšãSetãªããžã§ã¯ãå ããå€ãåé€ã§ãã
const fruits = new Set(["apple", "orange"]);
fruits.delete("orange");
console.log(fruits);
> Set {"apple"}
clear ã¡ãœããã¯ãSetãªããžã§ã¯ããä¿æããå€ããã¹ãŠåé€ããã
const fruits = new Set(["apple", "orange"]);
fruits.clear();
console.log(fruits);
> Set { }
has ã¡ãœããã¯ãSetãªããžã§ã¯ãå ã«äžèŽããå€ãååšãããã確èªããã
const fruits = new Set(["apple", "orange"]);
console.log(fruits.has("orange"));
> true
console.log(fruits.has("banana"));
> false
ã³ãŒã«ããã¯é¢æ°ãåŒæ°ã«åãSetã®ã¡ãœããã¯ãforEachã§ãã
Setãªããžã§ã¯ãã®forEach ã¡ãœããã¯ãé
åã®forEachã¡ãœãããšåæ§ã«ã³ãŒã«ããã¯é¢æ°ã䜿ã£ãŠã
ã«ãŒãåŠçãè¡ãã
[æ§æ] forEachã®èšæ³
Set ãªããžã§ã¯ã.forEach(function(value, sameValue, set) {
/* Setã®åèŠçŽ ã䜿ã£ãåŠç */
}, [_this])
value: Setã®å€ã1ã€ãã€æž¡ãããã
sameValue: valueãšåãå€ãæž¡ãããã
set: Setãªããžã§ã¯ãèªäœãæž¡ãããã
_this: ã³ãŒã«ããã¯é¢æ°å
ã®thisã®åç
§å
ãèšå®ããã
Setã®å Žåãã³ãŒã«ããã¯é¢æ°ã®ç¬¬äžåŒæ°valueãšç¬¬äºåŒæ°sameValueã¯ãããããåãå€ãæž¡ãããã
const set = new Set(["å€1", "å€2"]);
set.forEach((value, sameValue, set) => {
console.log(`value: [${value}],sameValue: [${sameValue}],set: `, set);
});
> value: [å€1],sameValue: [å€1],set: Set(2) {'å€1', 'å€2'}
> value: [å€2],sameValue: [å€2],set: Set(2) {'å€1', 'å€2'}
é åã®å Žåã¯lengthããããã£ããé åã®é·ã(èŠçŽ æ°)ãååŸããŸããããSetã®å Žåã¯sizeããããã£ã«é·ããæ ŒçŽãããŠããŸãã
const fruits = new Set(["apple", "orange"]);
console.log(fruits.size);
> 2
ES6ã§è¿œå ãããMapã¯ãããŒãšå€ã察ã§ä¿æããã³ã¬ã¯ã·ã§ã³ã§ãã
ES5ãŸã§ã¯ããªããžã§ã¯ã{}ã䜿ã£ãŠãããŒãšå€ã察ã§ç®¡çããŠãããES6以éã§ã¯Mapã䜿ãããšã§ç®¡çã§ããã
Mapã¯æ¬¡ã®ããã«åæåã§ããã
const emptyMap = new Map();
console.log(emptyMap);
> Map { }
const convertedMap = new Map([
["ããŒ1", "å€1"],
["ããŒ2", "å€2"]
]);
console.log(convertedMap);
> Map { "ããŒ1": "å€1", "ããŒ2": "å€2" }
Mapã«ã¯æ¬¡ã®ãããªã¡ãœãããããã
setã¡ãœããã䜿ããšãMapãªããžã§ã¯ãã«å¯ŸããŠå€ãè¿œå ã§ããã
Mapãªããžã§ã¯ãã®ããŒã«ã¯ãæåå以å€ã«ãæ°å€ãçåœå€ããªããžã§ã¯ããªã©ããã¹ãŠã®åã®å€ã䜿ãããšãã§ããã
const fruits = new Map();
fruits.set(1, "apple");
const emptyObj = { };
fruits.set(emptyObj, "orange");
fruits.set(true, "grap");
console.log(fruits);
> Map {1 => "apple", emptyObj => "orange", true => "grape"}
get ã¡ãœããã§å€ãååŸãã
const fruits = new Map();
const emptyObj = { };
fruits.set(emptyObj, "orange");
console.log(fruits.get(emptyObj));
> orange
ã»ç°ãªããªããžã§ã¯ãã§å€ãååŸããå Žå
const fruits = new Map();
const emptyObj = { };
fruits.set(emptyObj, "orange");
console.log(fruits.get({})); // emptyObjãšæ§é ãåãå¥ã®ãªããžã§ã¯ãã§å€ãååŸ
> undefined // å€ã¯ååŸã§ããªã
delete ã¡ãœããã䜿ããšãMapãªããžã§ã¯ãå ããå€ãåé€ã§ããã
const fruits = new Map([
[1, "apple"],
[false, "orange"],
]);
fruits.delete(false);
console.log(fruits);
> Map { 1 => "apple" }
clear ã¡ãœããã¯ãMapãªããžã§ã¯ãå ã«ä¿åããããŒãšå€ã®ãã¢ããã¹ãŠåé€ãã
const fruits = new Map([
[1, "apple"],
[false, "orange"],
]);
fruits.clear();
console.log(fruits);
> Map { }
has ã¡ãœããã¯ãMapãªããžã§ã¯ãå ã«ããŒãååšãããã確èªãã
const fruits = new Map([
[1, "apple"],
[false, "orange"],
]);
console.log(fruits.has(false));
> true
console.log(fruits.has(2));
> false
ã³ãŒã«ããã¯é¢æ°ãåŒæ°ã«åãMapã®ã¡ãœãããforEachã
Mapã®forEach ã¡ãœããã¯ãé åã®forEachãšåæ§ã«ãã³ãŒã«ããã¯é¢æ°ã䜿ã£ãŠã«ãŒãåŠçãè¡ãã
Mapãªããžã§ã¯ã.forEach(function(value, key, map) {
/* Mapã®åèŠçŽ ã䜿ã£ãåŠç */
}[,_this]);
value: Mapã®å€ã1ã€ãã€æž¡ãããã
key: Mapã®ããŒã1ã€ãã€æž¡ãããã
map: Mapãªããžã§ã¯ãèªäœãæž¡ãããã
_this: ã³ãŒã«ããã¯é¢æ°å
ã®thisã®åç
§å
ãèšå®ããã
(䜿çšäŸ)
const map = new Map([
["ããŒ1", "å€1"],
["ããŒ2", "å€2"],
]);
map.forEach((value, key, map) => {
console.log(`value: [${value}], key: [${key}], map: `, map);
});
> value: [å€1], key: [ããŒ1], map: Map(2) {'ããŒ1' => 'å€1', 'ããŒ2' => 'å€2'}
> value: [å€2], key: [ããŒ2], map: Map(2) {'ããŒ1' => 'å€1', 'ããŒ2' => 'å€2'}
Mapãªããžã§ã¯ãã§ã¯ãSetãšåæ§ãsizeããããã£ã«Mapã®é·ãã®æ å ±ãä¿æããŠããã
const fruits = new Map();
fruits.set(1, "apple");
const emptyObj = { };
fruits.set(emptyObj, "orange");
console.log(fruits.size);
> 2
Mapãããªããžã§ã¯ã{ }ã«å€æããã«ã¯ã次ã®ããã«èšè¿°ããã
[æ§æ] Mapãããªããžã§ã¯ããžå€æ
const ãªããžã§ã¯ã = Object.fromEntries(Mapãªããžã§ã¯ã);
(䜿çšäŸ)
const map = new Map([
["ããŒ1", "å€1"],
["ããŒ2", "å€2"],
]);
const obj = Object.fromEntries(map);
console.log(obj["ããŒ1"]);
> å€1
- Mapã®å ŽåãããŒã«æåå以å€ã䜿çšå¯èœ
- Mapã®å Žåãfor...ofæã䜿ã£ãç¹°ãè¿ãåŠçãèšè¿°å¯èœ
- Mapã®å ŽåãsizeãéããŠé·ããååŸå¯èœ
- Mapã®å Žåãã¡ãœãããä¿æã§ããªã
WeakMap ã¯ãããŒã«ãªããžã§ã¯ãã®ã¿äœ¿çšå¯èœãªã³ã¬ã¯ã·ã§ã³ã§ãã
WeakMapã¯æ¬¡ã®ããã«ããŠåæåãã
const wm = new WeakMap();
WeakMapã«ã¯æ¬¡ã®ãããªã¡ãœãããããã
set ã¡ãœããã§ãWeakMapãªããžã§ã¯ãã«å¯ŸããŠããŒãšå€ãç»é²ããã
const wm = new WeakMap();
let keyObj = { };
wm.set(keyObj, "value");
console.log(wm);
> WeakMap {{âŠ} => 'value'}
wm.set(1, "value2"); // ãªããžã§ã¯ã以å€ãããŒã«ãããšãšã©ãŒãçºçããã
> VM120:1 Uncaught TypeError: Invalid value used as weak map key
> at WeakMap.set (<anonymous>)
> at <anonymous>:1:4
get ã¡ãœããã¯ãç¹å®ã®ããŒã«å¯Ÿå¿ããå€ãååŸãã
const wm = new WeakMap();
let keyObj = {};
wm.set(keyObj, "value");
console.log(wm.get(keyObj));
> value
delete ã¡ãœããã¯ãç¹å®ã®ããŒã®å€ãåé€ãã
const wm = new WeakMap();
let keyObj = { };
wm.set(keyObj, "value");
wm.delete(keyObj);
console.log(wm);
> WeakMap { }
has ã¡ãœããã¯ãWeakMapå ã«äžèŽããããŒãªããžã§ã¯ããååšããã°trueãè¿ãã
const wm = new WeakMap();
let keyObj = { };
wm.set(keyObj, "value");
console.log(wm.has(keyObj));
> true
console.log(wm.has({}));
> false
WeakMapã¯å埩å¯èœãªãªããžã§ã¯ãã§ã¯ãªããããfor...ofæãªã©ã䜿ã£ãå埩åŠçã¯ã§ããªãã