-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": "[email protected]", "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..

5
  • 5
    Can you show us the structure of the data you're starting with? Commented Apr 8, 2013 at 10:27
  • Please don't confuse JavaScript objects with JSON objects. Commented Apr 8, 2013 at 10:27
  • @AnthonyGrist I have made the edit. Commented Apr 8, 2013 at 10:32
  • You should have a look at this answer: stackoverflow.com/a/351723/218196. Commented Apr 8, 2013 at 10:36
  • @faizanjehangir What I really want to see is what colValues looks like; without knowing that it's difficult to tell how you go from that to the structure you want. Commented Apr 8, 2013 at 10:38

3 Answers 3

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 
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what is needed, but I need an object encapsulate all the fields of values..
0
for (var key in keyValuePairs) {
  if (keyValuePairs.hasOwnProperty(key)) {
    data.push({ id: key, values:keyValuePairs[key]);
  }
}

hasOwnProperty filters out prototype properties

Comments

-1

You could do something like this:

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

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

Comments

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.