Using lodash, why is the map method within flow not working?

The answer applies for the standard version of lodash. Please see @Vlaz's answer for a look at the functional programming version of lodash.


When you write _.map(user => user.uid) it is actually invoking the function at that time. What you're really attempting to do is to create function that is similar to _.map, but has one of its arguments already set.

Fortunately, Lodash has a built-in to handle this situation - _.partialRight

const userIds = _.flow(
  _.partialRight(_.map, user => user.uid)
  _.partialRight(_.filter, userId => userId !== currentUserId),
)(users);

Documentation


Alternatively if you wish to use plain JS rather than importing a new function, you can simply wrap it in an anonymous function to pass the arguments properly.

const userIds = _.flow(
  (users) => _.map(users, user => user.uid),
  (users) => _.filter(users, userId => userId !== currentUserId),
)(users);

It appears that you are not using the functional version of lodash.

var users = {
    123: {
      uid: 123
    },
    456: {
      uid: 456
    }
};
var currentUserId = 123;

const userIds = _.flow(
  _.map(user => user.uid),
  _.filter(userId => userId !== currentUserId)
)(users);

console.log("result with normal lodash", userIds);
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>

var users = {
    123: {
      uid: 123
    },
    456: {
      uid: 456
    }
};
var currentUserId = 123;

const userIds = _.flow(
  _.map(user => user.uid),
  _.filter(userId => userId !== currentUserId)
)(users);

console.log("result with lodash FP", userIds);
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>

As per the Lodash FP guide, you should be importing that into your files

// Load the fp build.
var fp = require('lodash/fp');

In your case, you can just import just map, filter, and flow but you should get their lodash FP variants.


Can do this with a simple native JS reduce of array created by Object#values() with no more code than using lodash

const users = {123: {uid: 123},456: {uid: 456}};

const currId = 123;


const userIds = Object.values(users)
            .reduce((a, {uid}) => uid !== currId ? a.concat(uid): a, [])
    

console.log(userIds)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>