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

Erase by integer index #1018

Closed
zkelo opened this issue Mar 20, 2018 · 3 comments
Closed

Erase by integer index #1018

zkelo opened this issue Mar 20, 2018 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@zkelo
Copy link

zkelo commented Mar 20, 2018

I have this object.

json obj = { {"Foo", 1}, {"Bar", 2} };

In some cases I have only integer index of Foo (i.e. 0) or Bar (i.e. 1), not a key like "Foo" or "Bar". And I'm need to erase "Bar" using only integer index. If I'm trying to erase "Bar" with using integer index, I'm getting an exception.

So, how I can (if it's possible) erase "Bar" using integer index? Or I need to use cycle like this for (auto it : obj) to find associative key by integer in first?

@gregmarr
Copy link
Contributor

How do you have an integer index into a map? Where did the index come from? There is no guarantee, given the code that you posted, that the data is in the same order in the map as in the source code.

@zkelo
Copy link
Author

zkelo commented Mar 20, 2018

I'm using items() to make list of obj items, after that I make this list visible for user. Indexes of this list is a valid indexes of json object. And user can choose one list item for delete.

@gregmarr
Copy link
Contributor

Seems like your options are include the key data in the list, keep a vector of keys and get the key from the vector using the list index, or iterate through the items again until you get to the proper index, as there is no method that I know of to erase a map entry by index.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 20, 2018
@zkelo zkelo closed this as completed Mar 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants