Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
details: { languages: [{}], other: undefined } – EricG Aug 7 '12 at 19:39
2  
It's okay to answer your own question, but please pose the question as a question, and post a separate, actual answer. meta.stackoverflow.com/q/17463/133242 – Matt Ball Aug 7 '12 at 19:39
1  
"This is really not a question." Cool story bro? – Matthew Blancarte Aug 7 '12 at 19:42
Chris,maybe you post at code review. – flapjack Aug 7 '12 at 20:27

migrated from stackoverflow.com Aug 7 '12 at 23:58

2 Answers

up vote 1 down vote accepted
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));
share|improve this answer
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"} ]; – Chris Wagnor Aug 8 '12 at 20:34

How is this?

var movies = [{
    title: "ice age",
    details: {
        languages: {
            english: {
                year: "2012", 
                other: undefined
            },
            french: {
                year: "2012", 
                other: undefined
            },
            german: {
                year: "2012", 
                other: undefined
            }
        },
        detail2: {}
    },
    "movie2": {
        languages: {
            english: {
                year: "2012", 
                other: undefined
            },
            french: {
                year: "2012", 
                other: undefined
            },
            german: {
                year: "2012", 
                other: undefined
            }
        },
        detail2: {}
    }]
}
share|improve this answer
What are you trying to say here? – Chris Wagnor Aug 7 '12 at 19:49
Haha I didnt read accurate, I was trying to post a better structure :p – EricG Aug 7 '12 at 19:54

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.