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

Removing item from array #56

Closed
drewpts opened this issue Apr 8, 2015 · 8 comments
Closed

Removing item from array #56

drewpts opened this issue Apr 8, 2015 · 8 comments

Comments

@drewpts
Copy link

drewpts commented Apr 8, 2015

Hi. Thanks for great lib!
Is there support for removing item/items from array?

E.g.:

        json j;
        j["name"] = { "testname", "yetanothername" };
        j["name"].erase("testname");
$ ./a.out 
terminate called after throwing an instance of 'std::runtime_error'
what():  cannot use at with array
@mkwork
Copy link

mkwork commented Apr 8, 2015

Anyway, copy-pasted code detected. There are lying comments and wrong error message in this method.

@nlohmann
Copy link
Owner

nlohmann commented Apr 8, 2015

There is currently no function to delete elements from an array. This should be offered to have a "symmetrical" feature set compared to objects.

The text of the exception you reported is correct: the type of j["name"] is an array, therefore "cannot use at with array" is correct.

@mkwork
Copy link

mkwork commented Apr 8, 2015

Hm, but there isn't used at()? Or i missunderstood you.

@nlohmann
Copy link
Owner

nlohmann commented Apr 8, 2015

Oops... I'll fix that... :-)

@nlohmann
Copy link
Owner

nlohmann commented Apr 8, 2015

Fixed the comment in 1bdb6ac.

@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2015

84195da implements an erase functions for arrays:

void erase(const size_type pos)

This function removes the element at the given index pos. All following elements are moved forward. If pos is an invalid array index, std::out_of_range is thrown.

Compared to the initial example, I decided against passing a value to delete, because a value can occur multiple times and the result could be unexpected.

I hope this helps.

@nlohmann nlohmann closed this as completed Apr 9, 2015
@drewpts
Copy link
Author

drewpts commented Apr 9, 2015

@nlohmann my thanks!
I'll check it up by my side.

@mkwork
Copy link

mkwork commented Apr 9, 2015

Wow, thanks for so fast issue fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants