Let's say that I have a uni dimensional dataset like this:
let data = {"adam":19,"james":22,"john":24}
and some HTML like this
<span id="person" data-path="adam">
I can fun a function like this to get the age for the record adam
and then inset it into the element with the id
of person
let person = document.getElementById('person')
let name = person.dataset.path
let age = data[path]
person.innerHTML = age
I understand that we can use dot (.
) within a data-attribute, but I can not use a similar technique as above, where I use a data-attribute as a key to access a multidimensional dataset.
For example, let's say this is my dataset
let multi = {
"adam": {
"age": 19,
"color": "red"
}
}
and my HTML is
<span id="person" data-path="adam.age">
I would expect to be able to access the value of "age"
seeing as I can manually access it as multi.adam.age
but this does not work.
Does anyone know why using a .
from a variable to access multidimensional dataset does not work?