Reduce for Objects?
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:8
- Comments:9 (9 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
#257, #364, #546, #625, #656, #920, #1067
The problem is this sort of concern:
obj1
andobj2
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:
#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.
@CrossEye
We’ve been over this before 😄 But as was discussed in #1067 we’re already violating the law:
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
andR.toPairs
This also has advantage that
R.toPairs
can be used withR.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
.