Skip to content
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

Setting multiple values #20

Closed
json2d opened this issue May 4, 2018 · 4 comments
Closed

Setting multiple values #20

json2d opened this issue May 4, 2018 · 4 comments

Comments

@json2d
Copy link

json2d commented May 4, 2018

So I'm trying to set multiple values on the object, and here's what I'm using:

const object = { foo: 1, bar: 2 };

let chainA = set("foo", 2, object)
let chainB = set("bar", 3, chainA)

console.log(chainB); // { foo:2, bar:3 }

This gets pretty unwieldy the more values I need to set, and my guess that its not very optimal. It'd be great to add better support for this use case, however I'm not sure what's the best way to add it to the existing api.

set and other methods already have a fuzzy arg with an array type that does something else:

console.log(set("foo[0].bar", "quz", object)); // {foo: [{bar: 'quz'}]}
console.log(set(["foo", 0, "bar"], "quz", object)); // {foo: [{bar: 'quz'}]}

Here's one option for overloading set:

const object = { foo: 1, bar: 2 };

const changes = [
  ["foo", 2], // coupling first 2 args
  ["bar", 3].
]

const changedObject = set(changes, object);

Thoughts?

@planttheidea
Copy link
Owner

planttheidea commented May 5, 2018

Couldn't this be accomplished with a simple utility function in your project? Using your example code:

const setAll = (changes, object) => changes.reduce((newObject, change) => set(...change, newObject), object);

console.log(setAll([['foo', 2], ['bar', 3]], object)); // {foo: 2, bar: 3}

EDIT: or if your changes are at the same nesting level in your object, you could use merge instead of set:

console.log(merge({foo: 2, bar: 3}, object)); // {foo: 2, bar: 3}

@json2d
Copy link
Author

json2d commented May 9, 2018

Using reduce does work well, but it seems inefficient to call set many times in a row because of the immutability overhead.

@planttheidea
Copy link
Owner

planttheidea commented May 9, 2018

So...a couple things here.

One, I'll fall back on the use of merge. It accomplishes what you're looking for so long as everything is at the same depth.

Two... It's all relative. How many properties are you updating? How nested are these keys? I understand what you're saying, but it's conceptual ... Unless you are updating hundreds of deeply nested keys at once, the execution time difference is likely negligible for real world processing. And even in that edge case scenario, you're probably better off deep cloning and mutating the clone than setting a ton of specific properties immutably.

@json2d
Copy link
Author

json2d commented May 9, 2018

Thanks for the advice. I suppose you're right that it doesn't make that big of a difference in normal use cases. I'll look into using merge or the reduce pattern .

@json2d json2d closed this as completed May 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants