LoginSignup
1
2

More than 3 years have passed since last update.

[ES6] Arrayクラスメソッド超簡易まとめ

Posted at

Array.prototype.forEach

const number = [1, 2, 3, 4, 5];
let result = 0;

number.forEach((num) => {
    result += num;
})

console.log(result);
// => 15

Array.prototype.map

const number = [1, 2, 3, 4, 5];

const result = number.map((num) => {
    return num + 2
});

console.log(result);
// => [3,4,5,6,7]

Array.prototype.reduce

const number = [1, 2, 3, 4, 5];

const result = number.reduce((sum, num) => {
    return sum + num;
}, 0);

console.log(result);
// => 15

Array.prototype.some

const number = [1, 2, 3, 4, 5];

const result = number.some((num) => {
    return num == 3;
});

console.log(result);
// => True

Array.prototype.every

const number = [1, 2, 3, 4, 5];

const result = number.every((num) => {
    return num == 3;
});

console.log(result);
// => False

Array.prototype.filter

const number = [1, 2, 3, 4, 5];

const result = number.filter((num) => {
    return num % 2 == 0;
});

console.log(result);
// => [2, 4]

Array.prototype.find

const number = [1, 2, 3, 4, 5];

const result = number.find((num) => {
    return num % 2 == 0;
});

console.log(result);
// => 2

参考

MDN Array

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2