Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON array with nested objects, as below:

var cData = [{
    "name": "Jack Doe",
    "desc": "Jack",
    "values": [{
        "id": "615",
        "subject": "Physics",
        "Grade": "B"
    }, {
        "id": "616",
        "subject": "Chemistry",
        "Grade": "A"
    }]
},
{
    "name": "Jane Doe",
    "desc": "Jane",
    "values": [{
        "id": "715",
        "subject": "Maths",
        "Grade": "A+"
    }]
},
{
    "name": "Jack Doe",
    "desc": "Jack",
    "values": [{
        "id": "617",
        "subject": "Maths",
        "Grade": "A"
    }]
},
{
    "name": "Jane Doe",
    "desc": "Jane",
    "values": [{
        "id": "716",
        "subject": "Physics",
        "Grade": "B"
    }]
}]

I want to consolidate objects in above array to

var cData = [{
    "name": "Jack Doe",
        "desc": "Jack",
        "values": [{
        "id": "615",
            "subject": "Physics",
            "Grade": "B"
    }, {
        "id": "616",
            "subject": "Chemistry",
            "Grade": "A"
    }, {
        "id": "617",
            "subject": "Maths",
            "Grade": "A"
    }]
},

{
    "name": "Jane Doe",
        "desc": "Jane",
        "values": [{
        "id": "715",
            "subject": "Maths",
            "Grade": "A+"
    }, {
        "id": "716",
            "subject": "Physics",
            "Grade": "B"
    }]
}]

If any one has any suggestions for me it'd be really great! jQuery methods are also welcome.

share|improve this question
1  
what have you tried so far? also are you looking to merge the records based on the name field or the description field? how should differences in the other field be handled? –  Ben McCormick Jun 5 at 19:18
 
If I am not wrong you want to merge object in reference to same "name"? –  pvnarula Jun 5 at 19:20
add comment

1 Answer

up vote 1 down vote accepted

You have to write function to merge array of objects under key and then get map values. Here it is:

Merge function:

function mergeArray(array) {
    var merged = {};
    $.each(array, function() {
        var item = this;
        // Use name as a key
        if (typeof merged[item.name] != 'undefined') {
            // merge values array
            $.merge(merged[item.name].values, item.values);
        }
        else {
            merged[item.name] = item;
       }
    });
    // get values from { key1: value1, key2: value2, ... } object
    return getObjectValues(merged);
}

Getting values from object:

function getObjectValues(obj) {
    var values = [];
    $.each(obj, function(key,valueObj){
        values.push(valueObj);
    });
    return values;
}

Here is working example.

share|improve this answer
1  
great, working perfectly fine when I did included this in my program . I was working on this last two days could not able to figureout. Thank you very much for quick solution. –  AnuNag Jun 6 at 4:57
add comment

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.