$ cnpm install reduce-first
Return early from reduce.
.find() returns the element from an array, and .reduce() loops over every element. reduceFirst combines these to return a transformation of the first item that returns a value.
yarn add reduce-first
npm install reduce-first
import reduceFirst from "reduce-first";
const list = ["a", "b", "c", "d", "e"];
reduceFirst(list, (value, index, arr) => {
if(value === "c") {
return `found ${value}`;
}
}); // "found c"
reduceFirst(list, (value, index, arr) => {
if(value === "x") {
return `found ${value}`;
}
}); // undefined
Copyright 2013 - present © cnpmjs.org | Home |