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.

\$\endgroup\$
3
  • \$\begingroup\$ details: { languages: [{}], other: undefined } \$\endgroup\$
    – EricG
    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\$
    – Matt Ball
    Commented Aug 7, 2012 at 19:39
  • 1
    \$\begingroup\$ "This is really not a question." Cool story bro? \$\endgroup\$
    – Matthew Blancarte
    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));
\$\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

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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