Lodash: _.reduce()(obj) from lodash/fp does not send in Key

Created on 13 Jan 2016  路  3Comments  路  Source: lodash/lodash

import { reduce } from 'lodash/fp';
import { reduce as normReduce } from 'lodash';

const testObj = { hello: 12, hi: 21 };

const red = reduce((arr, n, key) => {
  arr.push({ [n]: key });
  return arr;
}, []);

const redNorm = normReduce(testObj, (arr, n, key) => {
  arr.push({ [n]: key });
  return arr;
}, []);

const resultFP = red(testObj);

console.log(resultFP); // => [ { '12': undefined }, { '21': undefined } ]
console.log(redNorm); // => [ { '12': 'hello' }, { '21': 'hi' } ]

The FP version of reduce, as you can see, does not return the key correctly. Judging from the release notes, it should work like I imported it, right?

enhancement

Most helpful comment

We introduced the convert method to simply things:

var map = require('lodash/fp/map').convert({ 'cap': false });

There's also an ESLint rule to warn you when you declare too many args for your iteratee function:
jfmengels/eslint-plugin-lodash-fp/rules/no-extraneous-iteratee-args

All 3 comments

We introduced the convert method to simply things:

var map = require('lodash/fp/map').convert({ 'cap': false });

There's also an ESLint rule to warn you when you declare too many args for your iteratee function:
jfmengels/eslint-plugin-lodash-fp/rules/no-extraneous-iteratee-args

Why 'cap': true as default? Why not default as false so that it can match with nonfunctional programming definition?

Hi @dilipkumar2k6!

FP composition favors capping of callback arguments because many don't account for things like index, object parameters which can unintentionally complete curries with unexpected inputs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dwoldrich picture dwoldrich  路  3Comments

bryandowning picture bryandowning  路  3Comments

toranb picture toranb  路  3Comments

rtheunissen picture rtheunissen  路  3Comments

sarahquigley picture sarahquigley  路  3Comments