Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I add to a json array in javascript?

I have data being pulled in from various sources, each returning some form of json or similar, although, differently formatted each time. I need to get them all into one array, but I can't figure out how to do it.

The first set is an array like this:

[
 Object {id="70", type="ab", dateadded="12345678"},
 Object {id="85", type="ab", dateadded="87654321"}, ... more items ...
]

The second set is being pulled in from Facebook, and is like this:

[
 Object {id="12341234234", created_time="12345678"},
 Object {id="567856785678", created_time="87654321"}, ... more items ...
]

So, I need to alter the second set so that it has 'type', and it has 'dateadded' instead of 'created_time', and then I need to get this all into one array so it can be sorted on 'dateadded'.

I've no idea how to do this! Can anyone help?

Thanks!

share|improve this question
This is no valid JSON String, is it? – jantimon Aug 24 '11 at 11:28

2 Answers

up vote 2 down vote accepted

Assuming you have actual valid JSON instead of what you quoted above:

var jsonOld = '[{"id":"70","type":"ab","dateadded":"12345678"},{"id":"85","type":"ab","dateadded":"87654321"}]',
    jsonNew = '[{"id":"12341234234","created_time":"12345678"},{"id":"567856785678","created_time":"87654321"}]';

Then first parse these values into actual Javascript arrays:

var mainArr = JSON.parse(jsonOld),
    newArr = JSON.parse(jsonNew);

(If you already have actual Javascript arrays instead of JSON strings then skip the above step.)

Then just iterate over newArr and change the properties you need changed:

for (var i = 0, il = newArr.length; i < il; i++) {
    newArr[i].type = 'ab';
    newArr[i].dateadded = newArr[i].created_time;
    delete newArr[i].created_time;
}

And concatenate newArr into mainArr:

mainArr = mainArr.concat(newArr);

And sort on dateadded:

mainArr.sort(function(a, b) { return a.dateadded - b.dateadded; });

This will result in:

[{"id":"70","type":"ab","dateadded":"12345678"},
 {"id":"12341234234","type":"ab","dateadded":"12345678"},
 {"id":"85","type":"ab","dateadded":"87654321"},
 {"id":"567856785678","type":"ab","dateadded":"87654321"}]

See example

share|improve this answer
Thanks! Yeah, I mangled what I have above, but the data is in the form you described, so this should work. Thanks again. – Sharon Aug 24 '11 at 13:44

Use the first array's push() method:

// for each item in second array
    firstArray.push(convert(item));

function convert(obj) {
    // Convert obj into format compatible with first array and return it
}

Hope this helps.

share|improve this answer
+1 Quicker, more concise solution to my problem. Brevity FTW! – Rap Nov 29 '11 at 20:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.