-1

I want to populate an array of objects, so that the array should be attached to a grid. This is the format that I require:

data.push({ id: 1, values: { "country": "uk", "age": 33, "name": "Duke", "firstname": "Patience", "height": 1.842, "email": "patience.duke@gmail.com", "lastvisit": "11\/12\/2002" } });

I have key-value pairs of values, I am not sure exactly how would I be constructing the above object in a loop...??

EDIT:

I have this so far, but I am only adding values, not their respective keys:

var recordValues = [];
//for loop iterator
recordValues.push(colValues[colCount]);
//end for

data.push({ id: recordID, values: recordValues });

colValues contain the following: "uk", 33, "Duke"...all the possible values in an object..

faizanjehangir
  • 2,771
  • 6
  • 45
  • 83

3 Answers3

0

If understand you correctly:

var data = [];
for (var i = 0; i < people.length; i++) {
    var person = people[i];
    data.push({ 
        id : i + 1,
        values : { 
            country : person.country, 
            age : person.age, 
            name : person.surname, 
            firstname : person.firstname, 
            height : person.height, 
            email : person.email,
            lastvisit : person.lastvisit 
        }
    });
}
azz
  • 5,852
  • 3
  • 30
  • 58
0
for (var key in keyValuePairs) {
  if (keyValuePairs.hasOwnProperty(key)) {
    data.push({ id: key, values:keyValuePairs[key]);
  }
}

hasOwnProperty filters out prototype properties

Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236
-1

You could do something like this:

var recordValues = [];
for (var value in colValues) {
   recordValues.push(colValues[value]);
}

data.push({ id: recordID, values: recordValues });
nemo
  • 1,675
  • 10
  • 16