0

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.
3
  • What is this notation? In JS, [Object] is an array containing the builtin object constructor, and Array[foo] looks up the property on the Array constructor whose name is the result of evaluating foo. Commented Nov 2, 2012 at 21:14
  • 'then the array of Objects, data.links, are displayed as "undefined"' - Displayed how? Please show more of your code, and perhaps a more real-world example of your data that is actually valid JS. For the data you show, after doing data=data[0] if you say data.links it will give you the links array, so then you'd have to access the individual links with data.links[0], etc. (By the way, there is no JSON anywhere in your question - JSON is a string format.) Commented Nov 2, 2012 at 21:15
  • @nnnnnn: I edited the questions to clarify things. Commented Nov 2, 2012 at 21:35

1 Answer 1

1

A couple of solutions:

If you control the JSON output, simply remove the enclosing brackets [] those are basically wrapping your object in an array.

data = { nodes: [...] ,links: [...] };

If you don't control the JSON just simply assign the zero index of the array to the variable you actually want to work with.

json = [ { nodes: [...] ,links: [...] } ];
data = json[0];

Unfortunately, 'links' is an array. To access a member of that array, you will need to access its index value.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, directly setting it without the brackets works fine. However, I still have the same issue with the undefined values in the nodes and links arrays when looking at the object in developer tools. For example: data.nodes[1].name = "undefined".
@graphmeter - That's because nodes is an array and doesn't have a name property - the items in the array have a name property (presumably; you don't show it in the question). You have to say data.nodes[0].name for the first one; use a variable in a loop to access each in turn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.