I have JSON array with one object consisting of nodes and links.
data = [Object]=[ { nodes: Array[..] ,links: Array[…] } ]
This is all fine, but for accessing the links for example I have to use data[0].links, which is a bit annoying. I would like the array to be an object, so that data.links gives access to the links. I have tried to set:
data = data[0];
But then the array of Objects, data.links, are displayed as "undefined".It seems like when a specific element is accessed the value is displayed, for example data.links[3].name. Why is that?
Edit:
More specifically:
if data = [ { nodes: Array[... ] ,links: Array[...] } ] =>
console.log(data[0].links); //shows the data[0].links[0].name = value in the console
if data = { nodes: Array[... ] ,links: Array[...] } =>
console.log(data.links); //shows data[0].links[0].name = undefined
but interestingly
console.log(data.links[0].name); //shows the correct value.
[Object]is an array containing the builtin object constructor, andArray[foo]looks up the property on theArrayconstructor whose name is the result of evaluatingfoo.data=data[0]if you saydata.linksit will give you the links array, so then you'd have to access the individual links withdata.links[0], etc. (By the way, there is no JSON anywhere in your question - JSON is a string format.)