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

Can't update key, unless in array, with dot.str #34

Closed
DaveBitter opened this issue Jun 8, 2018 · 3 comments
Closed

Can't update key, unless in array, with dot.str #34

DaveBitter opened this issue Jun 8, 2018 · 3 comments

Comments

@DaveBitter
Copy link

Hi, I just found out that if you want to update a key in an object with dot.str you can't do that:

const original = {
    a: 1,
    b: 2
}

dot.str("a", "Some string", original)

// results in:
// {
// 	a: 1,
// 	b: 2
// }

But while updating a key in an object in an array it works perfectly fine:

const original = {
    arr: [{
        a: 1,
	b: 2
    }]
};

dot.str('arr.0.a', "Some string", original);

// results in:
// {
//   arr: [{
// 		a: "Some string",
// 		b: 2
// 	}]
// }

I can imagine that you can update the key of an object in an array because the order might have changed and it could be an other object. It would be great to either do the same for both or console.warn if you try to this. This should also be in the docs.

Cheers

@itaibaruch
Copy link

itaibaruch commented Mar 29, 2019

Hi, a quick solution it to remove the key and then use dot.str:

const original = {
    a: 1,
    b: 2
}
dot.remove("a", original)
dot.str("a", "Some string", original)

// results in:
// {
// 	a: "Some string",
// 	b: 2
// }

@DaveBitter
Copy link
Author

Hi, thanks for the suggestion! This seems like a good workaround indeed. Is there a reason why you wouldn't be able to do it directly?

@Mayhem93
Copy link

The issue is not specific only to this case. Generally, dot.str() doesn't work with root level fields:
image

Can we get this fixed ? As it turns it out, it's actually a bug, and root level fields should be able to be accessed (at least through the str function)

@rhalff rhalff closed this as completed in fa5fe12 Jul 18, 2019
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

3 participants