I have the following Javascript object:
var data = {
"customerSiteName": "Caldwell Sugar",
"AgreementID": "0",
"AgreementType": "",
"AgreementTypeID": {
"1": "Percentage Support",
"2": "Consignment Support",
"7": "Mobile Solutions",
"9": "SmartGlance Subscription",
"10": "Customer FIRST Lite",
"11": "Solution Support - LPS",
"12": "InSight Subscription"
},
"ProgramLevel": "",
"CFProgramLevelID": [
[1, "Primary"],
[2, "Standard"],
[3, "Premium"],
[4, "Elite"]
],
"DistributorID": "16",
"StartDate": "",
"EndDate": ""
};
I need to convert the key CFProgramLevelID which is an Array into a one dimension object. This is what I have tried so far:
$.each(data, function(key, value) {
if (value !== null && value instanceof Array) {
var obj = dataObj = value.reduce((p, c, i) => (Array.isArray(c) && (p[i] ={[c.length - 1]: c[c.length - 1]}), p), {});
console.log(obj);
}
});
But each value in CFProgramLevelID is converted to an object returning in this:
Object {0: Object, 1: Object, 2: Object, 3: Object}
0: Object
1: "Primary"
__proto__: Object
1: Object
1: "Standard"
__proto__: Object
2: Object
1: "Premium"
__proto__: Object
3: Object
1: "Elite"
__proto__: Object
What I want to get is as follow:
"CFProgramLevelID": {
"1": "Primary",
"2": "Standard",
"3": "Premium",
"4": "Elite"
}
What I am doing wrong?
I forgot to mention I have created a jsFiddle here