3
\$\begingroup\$

This is really not a question. I was looking up this solution but couldn't find it anywhere. Thought of posting it here so it may save someone's time.

Here was my issue: I had a JSON coming from server which was not nested. Lets take example of Movies:

var movies = [
 {"name":"Ice Age 3", "language":"English", "year":"2012"},
 {"name":"Ice Age 3", "language":"French", "year":"2011"},
 {"name":"Ice Age 3", "language":"German", "year":"2013"}
];

Now effectively speaking, there is no need for server to return it like this, however this was my case and since I was using jquery-template I wanted to have it as a nested JSON object. Something like this:

var movies = [{
 "name": "Ice Age 3",
 "details": [
 {"language": "English", "year": "2012"}, 
 {"language": "French", "year": "2011"}, 
 {"language": "German", "year": "2013"}
 ]
}];

Here is the code which I wrote to convert it:

function convert(arr) {
 var convertedArray = [];
 $(arr).each(function () {
 var found = false;
 for (var i = 0; i < convertedArray.length; i++) {
 if (convertedArray[i].name === this.name) {
 found = true;
 convertedArray[i].details.push({
 "language": this.language,
 "year": this.year
 });
 break;
 }
 }
 if (!found) {
 convertedArray.push({
 "name": this.name,
 "details": [{
 "language": this.language,
 "year": this.year
 }]
 });
 }
 });
 return convertedArray;
}

I am sure there would be a better approach for this. But for me it worked.

asked Aug 7, 2012 at 19:37
\$\endgroup\$
3
  • \$\begingroup\$ details: { languages: [{}], other: undefined } \$\endgroup\$ Commented Aug 7, 2012 at 19:39
  • 2
    \$\begingroup\$ It's okay to answer your own question, but please pose the question as a question, and post a separate, actual answer. meta.stackexchange.com/q/17463/133242 \$\endgroup\$ Commented Aug 7, 2012 at 19:39
  • 1
    \$\begingroup\$ "This is really not a question." Cool story bro? \$\endgroup\$ Commented Aug 7, 2012 at 19:42

1 Answer 1

2
\$\begingroup\$
var found = false;

Using flag is not good practice.

This is better solution:

var movies = [
 {"name":"Ice Age 3", "language":"English", "year":"2012"},
 {"name":"Ice Age 3", "language":"French", "year":"2011"},
 {"name":"Ice Age 3", "language":"German", "year":"2013"},
 {"name":"Ice Age 4", "language":"German", "year":"2013"}
];
function convert(arr) {
 return $.map(unique(arr), function(name){
 return {
 name: name,
 details: $.grep(arr, function(item){
 return item.name == name
 })
 } 
 });
}
function unique(arr) {
 var result = [];
 $.map(arr, function(item){
 if ($.inArray(item.name, result))
 result.push(item.name);
 })
 return result;
}
console.log(convert(movies));
answered Aug 7, 2012 at 20:29
\$\endgroup\$
1
  • \$\begingroup\$ Hi Ivan, Thanks for the wonderful code. However the following input displays the data incorrectly: var movies = [ {"name":"Ice Age 3", "language":"English", "year":"2012"}, {"name":"Ice Age 4", "language":"French", "year":"2011"}, {"name":"Ice Age 3", "language":"German", "year":"2013"}, {"name":"Ice Age 4", "language":"German", "year":"2013"}, {"name":"Ice Age 3", "language":"German", "year":"2014"} ]; \$\endgroup\$ Commented Aug 8, 2012 at 20:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.