[英]How to convert from multidimensional array to one dimension object in Javascript?
我有以下Javascript對象:
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": ""
};
我需要將作為數組的密鑰CFProgramLevelID
轉換為一維對象。 這是我到目前為止所嘗試的:
$.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);
}
});
但是CFProgramLevelID
每個值都轉換為返回的對象:
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
我想得到的是如下:
"CFProgramLevelID": {
"1": "Primary",
"2": "Standard",
"3": "Premium",
"4": "Elite"
}
我做錯了什么?
我忘了提到我在這里創建了一個jsFiddle
更新了您的代碼,請參閱下面的內容(請注意,每個循環都會影響您對JSON密鑰對值的任何數組值,而不僅僅是CFProgramLevelID):
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": "" }; $.each(data, function(key, value) { if (value !== null && value instanceof Array) { var obj = dataObj = value.reduce((p, c, i) => (Array.isArray(c) && (p[i + 1] = c[c.length - 1]), p), {}); data[key] = obj; console.log(obj); console.log(data); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
你可以這樣做;
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": "" }, result = data.CFProgramLevelID.reduce((r,sa) => Object.assign(r,{[sa[0]]:sa[1]}), {}); console.log(result);
請嘗試以下代碼。
var objectConstructor = {}.constructor; var data = {} object = { "billing_address": { "billingAddress": "d", "billingCity": "e", "billingState": "f" }, "shipping_address": { "shippingAddress": "a", "shippingCity": "b", "shippingState": "c" } } a(object); function a(obj, key) { if (!obj) { return; } else if (obj.constructor !== objectConstructor) { data[key] = obj; } else { Object.keys(obj).map((key, index) => { a(obj[key], key); }) } } console.log(data)
我希望它有所幫助。
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.