theJava
theJava

Reputation: 15034

getting all values in the row

var patientInfo = {}   
patientInfo.patientID = row.patientID;
patientInfo.firstName = row.firstName;
patientInfo.lastName = row.lastName;
patientInfo.dateOfBirth = row.DateOfBirth;
patientInfo.age = row.age;
patientInfo.gender = row.gender;

When I print I patientInfo, I get only one row's data... how can I get all row's data?

Upvotes: 0

Views: 206

Answers (2)

Felix Kling
Felix Kling

Reputation: 816700

Not quite sure what you mean. But assuming you are having this code in a loop, you are always overwriting the patientInfo data. Add them to an array:

infos = []

for(.....) {
    var patientInfo = {}   
    patientInfo.patientID = row.patientID;
    patientInfo.firstName = row.firstName;
    patientInfo.lastName = row.lastName;
    patientInfo.dateOfBirth = row.DateOfBirth;
    patientInfo.age = row.age;
    patientInfo.gender = row.gender;

    infos.push(patientInfo)
}

It would be good to know here row is coming from. Maybe you could just do:

infos = []

for(.....) {
    infos.push(row)
}

or maybe you don't have to do this at all. You have to provide more information.

More information => better answers.

Upvotes: 2

alex
alex

Reputation: 490373

To iterate over properties in an object, use for in.

for (var property in patientInfo) {
   alert(property);
}

Keep in mind, however, that if someone has augmented Object, its enumerable properties will be iterated over too.

You can mitigate this by using hasOwnProperty().

for (var property in patientInfo) {
   if ( ! patientInfo.hasOwnProperty(property)) {
       continue;
   }
   alert(property);
}

Alternatively, you could specify you own toString() method which formats the values however you want. Be sure to return a string.

Upvotes: 1

Related Questions