-3

I have an array in javascript and I can't access any data in it. The array prints out like this: console View

EDIT This is my code: The issue is it's not getting anything from data[x], and data is the name of my array

for (var x in data) {
    yaxis.push(data[x].dateAdded);
    xaxis.push(data[x].priority);
    text_hover.push(data[x].name + ' - ' + data[x].organType);
}

I guess my issue is that its telling me my length is 0, but when in fact it is 1

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3324236
  • 5
  • 1
  • 8

2 Answers2

1

To me this looks like your array has one item in it so

array.forEach( item => console.log( item.HLAType )) 

should work or

array[0].HLAType

if however as someone suggested you want to print all the properties on your object you need to do:

let obj = array[0];
Object.keys(obj).forEach( key => {
  console.log( obj[key] )  //prints the value of said key
})
Chris Noring
  • 471
  • 3
  • 9
0

It’s not really clear from your question what exactly – that is, which statement(s) – is troubling you. I’ll guess you’re searching for (for instance) a line like var objectsOrganSize = myArray[0].organSize; (if your array is called myArray).

Or iterating through it:

for(var i=0; i<myArray.length; i++){
    console.log(myArray[i].organSize);
}

If, on the other hand, you’re confusing arrays with objects and actually want to iterate through organSize, bloodType, etc., look here

EDIT

Your code looks suspiciously like an asynchronous request. Could it be that your loop is executed when the data has not returned yet from the server. Or are you maybe accessing it outside of the callback function, so data is undefined?

Community
  • 1
  • 1
bleistift2
  • 494
  • 5
  • 14
  • I’ve seen this answer getting 2 upvotes and 2 downvotes. Since I’m new to this site, I’d like to encourage the downvoters to leave a note on what I could have improved. – bleistift2 Apr 08 '17 at 23:14
  • 1
    I would guess the downvotes are because you've clearly stated its not clear what's being asked. If it's not clear what is being asked **don't answer**. If you dont have the reputation to comment you'll just have to find questions you can answer without clarification. – Nick is tired Apr 09 '17 at 12:35
  • @NickA Thanks for the clarification. – bleistift2 Apr 09 '17 at 13:12
  • @NIckA I don’t have enough reputation to comment on the question, so I’ll hope you’ll read this. With `data=[{key1 : 'value 1'}];`, `for(x in data) console.log(data[x])` yields `> Object { key1: "value 1" }`, whereas `for(x in data) console.log(x)` yields `> 0`. Unlike in Java, the for-in-loop returns the key, not the referenced object. – bleistift2 Apr 09 '17 at 13:18
  • JS isnt my best language and i'm not on my PC atm, but given you're updates to the answer following the question changes I feel the answer is more appropriate, I merely meant that for future reference you shoild refrain from answering a question until there are enough details to provide an appropriate answer, indeed this is difficult before you can comment but you should be able to find some good questions that can be answered without clarification that will provide enough reputation to comment in future. – Nick is tired Apr 09 '17 at 15:09
  • 1
    I fully got your point. Personally, I thought it was more appropriate to _try_ to help than not to help at all; but if things are handled here differently, I’ll try to void guessing an answer. Luckily, I’ve got the privilege by now. —— As for my second remark (the JS stuff), I was referencing your comment (which you seem to have deleted) on the question itself stating that the ``for(x in data)`` loop returned the object itself. – bleistift2 Apr 10 '17 at 10:04
  • Indeed, I had expected it to return the objects like a `for .. in` loop in python, however that was incorrect. – Nick is tired Apr 10 '17 at 10:11