Combining Nested Objects In A Json Array
I have a JSON array with nested objects, as below: var cData = [{ 'name': 'Jack Doe', 'desc': 'Jack', 'values': [{ 'id': '615', 'subject': 'Physics',
Solution 1:
You have to write function to merge array of objects under key and then get map values. Here it is:
Merge function:
functionmergeArray(array) {
var merged = {};
$.each(array, function() {
var item = this;
// Use name as a keyif (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, ... } objectreturngetObjectValues(merged);
}
Getting values from object:
functiongetObjectValues(obj) {
var values = [];
$.each(obj, function(key,valueObj){
values.push(valueObj);
});
return values;
}
Here is working example.
Post a Comment for "Combining Nested Objects In A Json Array"