4

I have this chunk of javascript code:

function arrayMapper(mappingFunc) {
  return items => items.map(mappingFunc);
}

function fooTransformer(tem) {
  return (...); // do something with item and returns a value
}

function barTransformer(tem) {
  return (...); // do something with item and returns a value
}

const foosTransformer = arrayMapper(fooTransformer);
const barsTransformer = arrayMapper(barTransformer);
(...)
foosTransformer([1, 2, 3]);

I was wondering if something like my arrayMapper function would exist natively in something like lodash, just to avoid reinventing the wheel.

8
  • did you search the docs of lodash? Commented May 17, 2017 at 16:07
  • Yes, of course, but didn't found anything Commented May 17, 2017 at 16:09
  • 4
    Reinventing the wheel? It's a one-line function! Am I missing something? Commented May 17, 2017 at 16:09
  • 1
    Your arrayMapper function is just a wrapper for the native Array.prototype.map function, so what's even the point of it? Commented May 17, 2017 at 16:16
  • Ok, maybe it's over-engineered and I don't even need the arrayMapper function, I could use : const foosTransformer = items => items.map(fooTransformer); directly... it's just that I have 5 or 6 functions like that. Commented May 17, 2017 at 16:23

2 Answers 2

2

What you're essentially doing is currying the map function (from the right).

Lodash allows you do to just that:

const mapper = _.curryRight(_.map);

const square = mapper(x => x ** 2);

console.log(
  square([1, 2, 3]) // [1, 4, 9]
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

That being said, it's certainly an overkill to bring in a whole library for such a small feature. Maybe if you're using npm, you could just npm install lodash.curryright lodash.map and use only those but that's still likely to be unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use chain method and then you can add map multiple times and at the end call value()

var arr = [1, 2, 3];
var add1 = e => e + 1;
var add2 = e => e + 2;

var result = _.chain(arr).map(add1).map(add2).value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

You could also use ES6 parameter destructuring and reduce() to create function that takes array and any number of callback function and call map method on that array with each function you provided.

var arr = [1, 2, 3];
var add1 = e => e + 1;
var add2 = e => e + 2;

function arrayMapper(arr, ...mappingFunc) {
  return mappingFunc.reduce((r, f) => (r = Array.prototype.map.call(r, f), r), arr)
}


var result = arrayMapper(arr, add1, add2)
console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.