Let's say I have this code:
const names = `
{
"Joe": {
"nat": "American",
"hair": "brown"
},
"Peter": {
"nat": "German",
"hair": "blond"
}
}
`;
const load = JSON.parse(names);
Is there a way I can console.log() only (for example) "Joe" without it also outputting the "nat" and "hair"?
You can use Object.keys() which returns an array of a given object's own enumerable property names
const names = `
{
"Joe": {
"nat": "American",
"hair": "brown"
},
"Peter": {
"nat": "German",
"hair": "blond"
}
}
`;
const load = JSON.parse(names);
console.log(Object.keys(load)[0]);
More about Object.keys - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys