2017-05-24 57 views
1

我有以下的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 here

回答

2

更新您的代码,请参见下面的(当心,这每一圈都会影响你有一个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>

1

你可以做如下;

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);

0

请尝试以下代码。

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)

我希望它能帮助。

谢谢。

+4

你应该解释你粘贴的代码,而不是做代码转储。 – Adam

+0

我通过往复将多维对象转换为一维对象。 我想我们可以对多维数组使用相同的方法。 如果稍微更改我的代码,它将适用于此问题。 – WDV329

相关问题