如何在Javascript中将多维数组转换为一维对象?

时间:2017-05-24 19:10:28

标签: javascript jquery arrays

我有以下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": ""
};

我需要将作为Array的键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

3 个答案:

答案 0 :(得分: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 :(得分:1)

您可以这样做;

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分: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)

我希望它有所帮助。

感谢。