Skip to content

Instantly share code, notes, and snippets.

@kurgm
Last active May 6, 2024 16:59
Show Gist options
  • Save kurgm/42ed8352349a379480debf750abbc7fc to your computer and use it in GitHub Desktop.
Save kurgm/42ed8352349a379480debf750abbc7fc to your computer and use it in GitHub Desktop.
配列のメソッドをreduceとreduceRightで再現する

map メソッドを reduce メソッドで再現する

const array = [1, 2, 3];
const doubled = array.map((element) => element * 2);

const doubledByReduce = array.reduce(
  (acc, element, i) => {
    acc[i] = element * 2;
    return acc;
  },
  new Array(array.length)
);

filter メソッドを reduce メソッドで再現する

const array = [1, 2, 3, 4, 5];
const odds = array.filter((element) => element % 2 === 1);

const oddsByReduce = array.reduce(
  (acc, element) => {
    if (element % 2 === 1) {
      acc.push(element);
    }
    return acc;
  },
  []
);

some メソッドを reduce メソッドで再現する

const array = [1, 2, 3, 4, 5];
const hasEven = array.some((element) => element % 2 === 0);

// 必ず配列全体を走査するため、一般には some メソッドより効率が悪い
const hasEvenByReduce = array.reduce(
  (acc, element) => acc || Boolean(element % 2 === 0),
  false
);

every メソッドを reduce メソッドで再現する

const array = [1, 2, 3, 4, 5];
const allOdd = array.every((element) => element % 2 === 1);

// 必ず配列全体を走査するため、一般には every メソッドより効率が悪い
const allOddByReduce = array.reduce(
  (acc, element) => acc && Boolean(element % 2 === 1),
  true
);

find メソッド, findIndex メソッド, findLast メソッド, findLastIndex メソッドを reduce メソッド, reduceRight メソッドで再現する

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

// find メソッド
const firstEven = array.find((element) => element % 2 === 0);

// 必ず配列全体を走査するため、一般には find メソッドより効率が悪い
const firstEvenByReduce = array
  .reduce(
    (acc, element) => {
      if (acc.found) return acc;
      if (element % 2 === 0)
        return { found: true, value: element };
      return acc;
    }
    { found: false, value: undefined }
  )
  .value;

// findIndex メソッド
const firstEvenIndex = array.findIndex((element) => element % 2 === 0);

// 必ず配列全体を走査するため、一般には findIndex メソッドより効率が悪い
const firstEvenIndexByReduce = array.reduce(
  (acc, element, i) => acc === -1 && element % 2 === 0 ? i : acc,
  -1
);

// findLast メソッド
const lastEven = array.findLast((element) => element % 2 === 0);

// 必ず配列全体を走査するため、一般には findLast メソッドより効率が悪い
const lastEvenByReduce = array
  .reduceRight(
    (acc, element) => {
      if (acc.found) return acc;
      if (element % 2 === 0)
        return { found: true, value: element };
      return acc;
    }
    { found: false, value: undefined }
  )
  .value;

// findLastIndex メソッド
const lastEvenIndex = array.findLastIndex((element) => element % 2 === 0);

// 必ず配列全体を走査するため、一般には findLastIndex メソッドより効率が悪い
const lastEvenIndexByReduce = array.reduceRight(
  (acc, element, i) => acc === -1 && element % 2 === 0 ? i : acc,
  -1
);

flatMap メソッドを reduce メソッドで再現する

const array = [1, 2, 3];

const repeated = array.flatMap((element) => new Array(element).fill(element));

const repeatedByReduce = array.reduce(
  (acc, element) => {
    acc.push(...new Array(element).fill(element));
    return acc;
  },
  []
);

includes メソッドを reduce メソッドで再現する

// includes メソッド
const array = [1, 2, 3, 4, 5];
const hasValue = array.includes(3);

// 必ず配列全体を走査するため、一般には includes メソッドより効率が悪い
const hasValueByReduce = array.reduce(
  (acc, element) => acc || element === 3,
  false
);

indexOf メソッド, lastIndexOf メソッドを reduce メソッド, reduceRight メソッドで再現する

// indexOf メソッド
const array = [1, 2, 3, 4, 5];
const firstIndex = array.indexOf(3);

// 必ず配列全体を走査するため、一般には indexOf メソッドより効率が悪い
const firstIndexByReduce = array.reduce(
  (acc, element, i) => acc === -1 && element === 3 ? i : acc,
  -1
);

// lastIndexOf メソッド
const lastIndex = array.lastIndexOf(3);

// 必ず配列全体を走査するため、一般には lastIndexOf メソッドより効率が悪い
const lastIndexByReduce = array.reduceRight(
  (acc, element, i) => acc === -1 && element === 3 ? i : acc,
  -1
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment