question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce for Objects?

See original GitHub issue

I find myself trying to turn Objects into other types pretty frequently and find myself doing something like:

Object.keys(myObj).reduce((acc, key) =>  doSomething(key, myObj[key]), someAccumulator)

the other part of the time I want to return early from the iteration and that ends up being the only time I really have to reach for a for loop.

What I’m hoping for is a function like:

reduceObject :: (b -> a -> String -> b) -> Object a -> b I know this isn’t strictly the same as ramda’s reduce so I can see adding a separate function to deal with this case.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:8
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
CrossEyecommented, Jan 11, 2017

#257, #364, #546, #625, #656, #920, #1067

The problem is this sort of concern:

var diff = function(x, y) {return y - x;}, 
    obj1 = {a: 1, b: 2, c: 3}, 
    obj2 = {b: 2, a: 1, c: 3};
_.reduce(obj1, diff); //=> 2
_.reduce(obj2, diff); //=> 4

obj1 and obj2 differ only in the order of key definition.

If we did something like that in Ramda, we’d violate a law that I’d like to maintain:

if R.equals(x, y), then R.equals(f(x), f(y)) for any f.

#1067 was a long discussion, and involved one proposal that would allow us to do this but was pretty controversial: apply our own sort to keys before any function where it might matter.

5reactions
paldepindcommented, Jan 14, 2017

@CrossEye

We’ve been over this before 😄 But as was discussed in #1067 we’re already violating the law:

R.equals({a: 1, b:2}, {b: 2, a: 1}); //=> true
R.equals(R.keys({a: 1, b:2}), R.keys({b: 2, a: 1})); //=> false

So, should we remove R.keys and the other functions you mention here? Why do these function get to stay?

The way I see it our users keeps asking us to add this function and we keep trying to convince them it’s bad for them.

To me the use case seems covered by R.values and R.toPairs

R.reduceRight(([val, acc]) => ..., R.toPairs(object));

This also has advantage that R.toPairs can be used with R.reduceWhile which might be useful for the early return you mention @jethrolarson.

But this also leads me to the question: If we’re already giving people something that is just as dangerous but just slightly less convenient why not give them the real thing?

I’m indifferent. I think a fine solution would be to add an example of reducing an object to the documentation for R.toPairs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Array.reduce With Objects - RedBit Development
This article will demonstrate a few ways that you can use JavaScript's `Array.reduce` method to transform objects.
Read more >
Javascript reduce() on Object - Stack Overflow
Second, reduce is an Array method, not an Object's one, and you can't rely on the order when you're iterating the properties of...
Read more >
Array.prototype.reduce() - JavaScript - MDN Web Docs
The reduce() method is an iterative method. It runs a "reducer" callback function over all elements in the array, in ascending-index order, and ......
Read more >
Using Array.prototype.reduce to form objects using JavaScript
prototype.reduce is a handy method for reducing datasets into meaningful values. One thing that isn't obvious is that you can reduce arrays to...
Read more >
JavaScript Array Reduce(): Explained With Syntax and ...
The array reduce in JavaScript is a predefined method used to reduce an array to a single value by passing a callback function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found