8000 _.replace(collection, match, new) · Issue #1022 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content

_.replace(collection, match, new) #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
octref opened this issue Mar 6, 2015 · 10 comments
Closed

_.replace(collection, match, new) #1022

octref opened this issue Mar 6, 2015 · 10 comments

Comments

@octref
Copy link
Contributor
octref commented Mar 6, 2015

Before adding the replace on StringWrapper in #1016, I thought about adding replace:

Replace matches in collection with new
match is a normal [predicate=_.identity]
new can be either:

  • A value with which matched items will be replaced
  • A function which takes in the old value and returns the new value.
_.replace(collection, match, new)

function isEven(x) { return x % 2 == 0; }
_.replace([1, 2, 3], isEven, 42)
// -> [1, 42, 3]
_.replace([1, 2, 3], isEven, function(x) { return x + 1; )
// -> [1, 3, 3]
_.replace([1, null, 3], _.isNull, 42)
// -> [1, 42, 3]

Why not just a _.map?
_.replace([1, null, 3], _.isNull, 42) looks really intuitive. The isNull can be any function you built to validate your data.

@octref octref changed the title _.replace(collection, match, new) _.replace(collection, match, new) Mar 6, 2015
@jdalton
Copy link
Member
jdalton commented Mar 6, 2015

Let's keep it simple with the chaining string sugar for now.
Though if/when we wouldn't use replace as its name.

@octref
Copy link
Contributor Author
octref commented Mar 6, 2015

Roger.

@jdalton
Copy link
Member
jdalton commented Mar 6, 2015

BTW I'm really digging your enthusiasm!

@octref
Copy link
Contributor Author
octref commented Mar 6, 2015

Thanks for you encouraging words!
Instead of trying to get a 4.0 in college I decided to do some more Open Source. My grade suffers a little bit but who cares as long as I can graduate. Coding is much more fun and rewarding :-)

@iCodeForBananas
Copy link

@jdalton I'm looking for this kind for this particular function right now actually. Did this function ever get merged? I read through a bunch of issues and commits but didn't find anything.

var newArr = _.replace(original, { id: 1 }, replacement);

http://codepen.io/michaeljcalkins/pen/xGjmzo?editors=001

@jdalton
Copy link
Member
jdalton commented Jul 11, 2015

@michaeljcalkins

Did this function ever get merged?

Nope, I opted to keep things simple and rely only on the chaining sugar for now.

@bcherny
Copy link
bcherny commented May 10, 2016

Immutable splice would be very helpful - similar to http://facebook.github.io/immutable-js/docs/#/List/updateIn

@jfmengels
Copy link
Contributor

There are two similar proposals (one being mine, though this one is the oldest): #1967 and #2053.

Differences: This one requests a function as the predicate, #2053 requested an index and #1967 either of those.

@bcherny
Copy link
bcherny commented May 10, 2016

@jfmengels thanks, I voted.

@lock
Copy link
lock bot commented Jan 19, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

5 participants
0