1

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

3 Answers 3

2

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

2 Comments

You should explain the code you've pasted instead of doing a code dump.
I converted a multidimensional object into a one dimension object, by reciprocating it. I think we can use the same method for multi-dimensional arrays. If change my code a little, it will work for this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.