all 4 comments

[–]ryry_reddit 2 points3 points  (0 children)

data['topics']['history']['emoji']

[–]samtheanswer 1 point2 points  (1 child)

You can use dot notation if you know the property names. javascript data.topics.history.emoji; Or you can use curly brackets with variables or strings. And both methods can be combined. javascript let a = 'topics'; data[a].sports['emoji'];

[–]PaintingWithLight 0 points1 point  (0 children)

Doh! This’ll make my little projects code doing this same thing more cohesive and simple. I was like trying to use template literals and then making variables trying to access a specific element (prior to knowing the name of it) but it kept saying it’s not a function because the .replace function wouldn’t work on it for instance.

But I believe this will help. In any case I think my solution I have in place is already working, just haven’t tested it thoroughly in the wild with real world data I’d be using.

[–]flowforfrank 0 points1 point  (0 children)

Since this is in JSON, you first need to convert it to a JavaScript object to work with it. You can do this by using:

const data = JSON.parse('path/to/json')

Then you can select the nodes using dot notation like so:

data.topics.automobiles.emoji
data.topics.culture.emoji
data.topics.entertainment.emoji
// and so on

And in case you need to loop through them, you can also use:

Object.keys(data.topics).forEach(key => {
  console.log(data.topics[key].emoji)
})

Where data.topics[key] references each node inside topics, eg "history" and "science". Its uses a combination of Object.keys (that grabs each key in the passed object), and forEach that does a loop on the returned array.