ReynierPM
ReynierPM

Reputation: 18660

How to convert from multidimensional array to one dimension object in Javascript?

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

Upvotes: 1

Views: 1225

Answers (3)

phoenix
phoenix

Reputation: 36

Please try the following code.

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)

I hope it helps.

Thanks.

Upvotes: 0

Woodrow
Woodrow

Reputation: 2832

Updated your code, please see below (be mindful that this each loop will affect any array values that you have for a JSON key pair value, not just for 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>

Upvotes: 2

Redu
Redu

Reputation: 26161

Your may do as follows;

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

Upvotes: 1

Related Questions