Jc John
Jc John

Reputation: 1859

Json array getting first data

I have an array which I declared as var myclinicsID = new Array(); and now, I push some data in it,when I alert it using alert(JSON.stringify(myclinicsID)) it gives me output of ["1","2","3","4"]

Now I want to use this for my function and when I look at in console, it gives me undefined, am I doing correct by my code :

getbarSeriesData(myclinicsID[0]['clinic_id'],data[i]['datemonths']);

I want to get myclinicsID first data element which is the value is 1

Upvotes: 1

Views: 206

Answers (2)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27192

Why myclinicsID[0]['clinic_id'] ? As there is nothing like clinic_id in your array.

Your array is single dimensional array. Hence, you can directly access the first element from an array using myclinicsID[0].

DEMO

var myclinicsID = new Array();
myclinicsID[0] = 1;
myclinicsID[1] = 2;
myclinicsID[2] = 3;
myclinicsID[3] = 4;

function getbarSeriesData(clientID) {
  console.log(clientID);
  alert(clientID);
}

getbarSeriesData(myclinicsID[0]);

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

myclinicsID[0]['clinic_id']

Should be

myclinicsID[0]

All you need the array index. When you say myclinicsID[0]['clinic_id'], that is trying to get the clinic_id property of "1" which is obvious undefined.

Upvotes: 2

Related Questions