1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[JS] 配列 / reduce

Last updated at Posted at 2021-03-27

reduceメソッド

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

arry.reduce(function(accu, curr) {
  console.log(accu, curr);
})

Image from Gyazo

currentには配列の値、
accumulationにはループの戻り値が入る


足し算

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

arry.reduce(function(accu, curr) {
  console.log(accu, curr);
  return accu + curr;
})

Image from Gyazo


掛け算

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

arry.reduce(function(accu, curr) {
  console.log(accu, curr);
  return accu * curr;
})

Image from Gyazo

このように、前のループの戻り値がaccuに渡ってくるというのがreduceメソッドです


第2引数に初期値を設定できる

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

arry.reduce(function(accu, curr) {
  console.log(accu, curr);
  return accu * curr;
}, 10);

Image from Gyazo


【目的】animationという文字列の値1つ1つに<>を付与したい

const str = 'animation';
const strArry = str.split('');

console.log(strArry);

const result = strArry.reduce((accu, curr) => {
  return accu + '<' + curr + '>';
})

console.log(result);

Image from Gyazo

accuの1ループ目は配列の最初の値が入るので、<>がついていません
2ループ目以降は戻り値が入ります


'<' + accu + '>'とすればいいかというと、そういうわけでもありません
<>が多くなり過ぎます

const str = 'animation';
const strArry = str.split('');

console.log(strArry);

const result = strArry.reduce((accu, curr) => {
  return '<' + accu + '>' + '<' + curr + '>';
})

console.log(result);

Image from Gyazo


第2引数に空文字を渡してあげると

const str = 'animation';
const strArry = str.split('');

console.log(strArry);

const result = strArry.reduce((accu, curr) => {
  return accu + '<' + curr + '>';
}, "")

console.log(result);

Image from Gyazo

ちょうど良くなります


戻り値の部分をテンプレートリテラルで

const str = 'animation';
const strArry = str.split('');

console.log(strArry);

const result = strArry.reduce((accu, curr) => {
  return `${accu}<${curr}>`;
}, "")

console.log(result);

こう書き直せます

Image from Gyazo

出力結果は同じです


console.log(accu);で中身を見てみると

const str = 'animation';
const strArry = str.split('');

console.log(strArry);

const result = strArry.reduce((accu, curr) => {
  console.log(accu);
  return accu + '<' + curr + '>';
}, "")

console.log(result);

Image from Gyazo

こんな感じになっています

reduce関数

const str = 'animation';
const strArry = str.split('');

function tag(accu, curr) {
  console.log(accu, curr);
  return `${accu}<${curr}>`;
}

function reduce(arry, callback, defaultValue) {
  let accu = defaultValue;
  for(let i = 0; i < arry.length; i++) {
    let curr = arry[i];
    callback(accu, curr);
  }
}

const result = reduce(strArry, tag, "");
console.log(result);

Image from Gyazo


const str = 'animation';
const strArry = str.split('');

function tag(accu, curr) {
  console.log(accu, curr);
  return `${accu}<${curr}>`;
}

function reduce(arry, callback, defaultValue) {
  let accu = defaultValue;
  for(let i = 0; i < arry.length; i++) {
    let curr = arry[i];
    accu = callback(accu, curr);
  }
}

const result = reduce(strArry, tag, "");
console.log(result);

Image from Gyazo


const str = 'animation';
const strArry = str.split('');

function tag(accu, curr) {
  console.log(accu, curr);
  return `${accu}<${curr}>`;
}

function reduce(arry, callback, defaultValue) {
  let accu = defaultValue;
  for(let i = 0; i < arry.length; i++) {
    let curr = arry[i];
    accu = callback(accu, curr);
  }

  return accu;
}

const result = reduce(strArry, tag, "");
console.log(result);

Image from Gyazo

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?