Non
Non

Reputation: 8589

How to convert an array to object in this special case?

I am converting some data in order to send it to the frontend in a readable way.

I just convert some XML data into JSON, and I am getting an object, but every value is key : ['value'], and I need it key: 'value'

I am sending the data like this res.status(200).json({dealersData}); where dealersData returns this

{
  "dealersData": [
    {
      "DealerId": [
        "1"
      ],
      "DealerName": [
        "Carmen"
      ],
      "NickName": [
        "Carmen"
      ],
      "Picture": [
        "00001.jpg"
      ],
      "Active": [
        "1"
      ],
      "LegalId": [
        "111111111"
      ],
      "TypeId": [
        "1"
      ]
    },
    {
      "DealerId": [
        "14"
      ],
      "DealerName": [
        "Jaz"
      ],
      "NickName": [
        "Jaz"
      ],
      "Active": [
        "1"
      ],
      "LegalId": [
        "111111187"
      ],
      "TypeId": [
        "1"
      ]
    }
  ]
}

so as you can see I am getting the data "TypeId":["1"] and so on, and I don't want like that, I want "TypeId" : 1, so what should I do to translate that arrays into objects hopefully using Lodash ?

Upvotes: 1

Views: 52

Answers (3)

alvarodms
alvarodms

Reputation: 721

If I get it right, you have arrays, but you don't need them, since you will always return just a single value, right?

If that's the case, you just have to call .toString() method on your arrays object. I would iterate over your dealersData to do that on all arrays:

for(var i = 0; i < dealersData.length; i++) {
   dealersData[i].DealerId = dealersData[i].DealerId.toString();
   dealersData[i].DealerName = dealersData[i].DealerName.toString();
   [...]
}

Upvotes: 1

pawel
pawel

Reputation: 36965

For each dealer iterate over the keys and replace the value with value[0].

dealersData = dealersData.map( function( dealer ){
    Object.keys( dealer ).forEach( function(key){
        dealer[key] = dealer[key][0];   
    })
     return dealer;
 });

http://jsfiddle.net/r38u4qag/1/ (or ES2015 fat arrow version: http://jsfiddle.net/r38u4qag/2/)

Upvotes: 3

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

This will recursively step through the object, changing arrays of length one to strings:

function process(obj) {
  for(var key in obj) {
    if(obj[key] instanceof Array && obj[key].length==1) {
      obj[key]= obj[key][0];
    }
    else if(obj[key] instanceof Object) {
      process(obj[key]);
    }
  }
} //process

function process(obj) {
  for(var key in obj) {
    if(obj[key] instanceof Array && obj[key].length==1) {
      obj[key]= obj[key][0];
    }
    else if(obj[key] instanceof Object) {
      process(obj[key]);
    }
  }
} //process

var obj= {
  "dealersData": [
    {
      "DealerId": [
        "1"
      ],
      "DealerName": [
        "Carmen"
      ],
      "NickName": [
        "Carmen"
      ],
      "Picture": [
        "00001.jpg"
      ],
      "Active": [
        "1"
      ],
      "LegalId": [
        "111111111"
      ],
      "TypeId": [
        "1"
      ]
    },
    {
      "DealerId": [
        "14"
      ],
      "DealerName": [
        "Jaz"
      ],
      "NickName": [
        "Jaz"
      ],
      "Active": [
        "1"
      ],
      "LegalId": [
        "111111187"
      ],
      "TypeId": [
        "1"
      ]
    }
  ]
}

process(obj);
document.querySelector('pre').innerHTML= JSON.stringify(obj,'\n',2);
<pre></pre>

Upvotes: 3

Related Questions