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

Object.keys() #459

Closed
ghost opened this issue Feb 16, 2017 · 2 comments
Closed

Object.keys() #459

ghost opened this issue Feb 16, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Feb 16, 2017

I have this in JS:

let myObject = {"pages": {"32746": {"data": "A data"}}};
console.log(myObject.pages[myObject.keys(myObject.pages)])

I readed the API documentation but I don't found this.
I tried with: j["pages"]["*"] but don't work.
Any help?

@nlohmann
Copy link
Owner

There is currently no such call in the library. Internally, we use std::map for objects, and I don't know a function to retrieve a map's keys without explicitly iterating over the map and collecting the keys.

Maybe this could be a feature for the future.

@nlohmann
Copy link
Owner

I think this is out of scope for a C++ library. There is no "cheap" solution to retrieve a std::map's keys, and a keys() function would hence be an naive loop that copies the keys into a container. And mostly, you would then need to iterate that container again to do anything useful with the keys - and this iteration could also have been done on the object itself, like

for (json::iterator it = myObject["pages"].begin();
     it != myObject["pages"].end(); ++it)
{
  std::cout << "this is a key: " << it.key() << "\n";
  // or do anything else with the key
}

or even nicer:

for (auto it : json::iterator_wrapper(myObject["pages"]))
{
  std::cout << "this is a key: " << it.key() << "\n";
  // or do anything else with the key
}

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

1 participant