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

_.set creates arrays when sees integres in paths (and this weird behaviour isn't documented) #1685

Closed
mik01aj opened this issue Dec 4, 2015 · 7 comments
Labels

Comments

@mik01aj
Copy link

mik01aj commented Dec 4, 2015

I just spent almost an hour debugging this.

var obj = {};
_.set(obj, ['collection', 1170798506, 'stuff'], 'value');
console.log(obj);

When I run this in Node, it hangs and then crashes. When I run this in browser, it works fine.

The problem is that when _.set sees an integer in path, it creates an array, and when Node's console.log sees that obj.collection.length is 1170798507, it hangs and then crashes saying "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory".

I can understand that this behaviour is usually convenient, but in situations like this it can cause major pain. I'd actually expect _.set to always create plain objects, but creating arrays would also be fine for me as long as it's documented.

@jdalton
Copy link
Member

jdalton commented Dec 4, 2015

Hi @mik01aj!

In v4 (due in Jan) we've added _.setWith to allow customization of values created.
I've added a doc note on the array creation behavior.

@jdalton jdalton closed this as completed Dec 4, 2015
@jdalton jdalton added the invalid label Dec 4, 2015
@megawac
Copy link
Contributor

megawac commented Dec 4, 2015

@jdalton slightly related on the topic of array setting set( <array>, <path>, <val> ), I think there should be a way to denote the next element (i.e. push)

@jdalton
Copy link
Member

jdalton commented Dec 4, 2015

I think there should be a way to denote the next element (i.e. push)

Ya, that's been brought up a couple times. I've been avoiding it because tips things it complicates things a bit more.

@mik01aj
Copy link
Author

mik01aj commented Dec 4, 2015

@megawac I think you could do this with _.setWith (then the adding logic is up to you).

@jdalton
Copy link
Member

jdalton commented Dec 4, 2015

@mik01aj

I think you could do this with _.setWith (then the adding logic is up to you).

Oh snao!

@megawac
Copy link
Contributor

megawac commented Dec 4, 2015

@mik01aj

I think you could do this with _.setWith (then the adding logic is up to you).

Sure, but I'm just anticipating this to be a common request

@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.
Labels
Development

No branches or pull requests

3 participants