最終更新日: 2026-07-24
TOP(About this memo)) > 一覧(JavaScript) > 配列操作・アロー関数など
var array = [2, 3, 5];
var num = array.map(function(value) {
// 配列の各値を3倍にする
return value * 3;
});
console.log(num);
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
// 1 + 2 + 3 + 4
console.log([1, 2, 3, 4].reduce((previousValue, currentValue) => previousValue + currentValue));
// expected output: 10
return Object
.keys(this.$route.query)
.filter(key => key !== "redirect_url")
.map(key => key + "=" + this.$route.query[key])
.join('&');
keys()で配列にしてから、mapの中でオブジェクトへキー経由でアクセスしている。redirect_urlがキーの場合を除外している。// 単一式の場合はブラケットやreturnを省略できる
const fn = (a, b) => a + b;
// ブラケットやreturnを省略してオブジェクトを返したい場合は`()`で囲む
const fn = (a, b) => ({ sum: a + b });
(function(name) {
console.log("Hello " + name)
})("taro");
// => "Hello taro"
// アロー関数を使うとこんな感じ
(name => console.log("Hello " + name))("taro");
// => "Hello taro"
thisの参照先が異なるが、アロー関数は関数が定義されたスコープ内のthisを参照する。
var module = (function() {
var count = 0;
return {
increment: function() {
count++;
},
show: function() {
console.log(count);
}
};
})();
module.show(); // 0
module.increment();
module.show(); // 1
console.log(count); // undefined
var arr = [1, 2, 3];
console.log(arr); // [1, 2, 3]
console.log(...arr); // 1 2 3