0

I do have object like this:

var pdfObj = {2:"value1",4:"value2",13:"value3"};

I want above object like this:

var pdfObj = {pdf:"value1",pdfs:"value2",pdfd:"value3"}

I got all the values but only thing I'm confused about is how to replace object key for example "2" with "pdf" in the object?

Keys in above object is the ids of one of my DB table and now I want to display key and value into list page, for that I fetched the particular name of the id "2": "pdf" from table and now I'll looped the object to display into list page but for that I want to replace the key(2) into key(pdf) so I can easily loop the object and display the name and its value.

Dhaval
  • 901
  • 3
  • 8
  • 26

1 Answers1

0

You can declare it directly like this:

pdfObj.pdf = pdfObj[2];
pdfObj.pdfs = pdfObj[4];
pdfObj.pdfd = pdfObj[13];

If you don't like there are duplicates, just create some temporary object, where you declare these keys and then just replace the new one with the old one:

var tmp = {};
tmp.pdf = pdfObj[2];
tmp.pdfs = pdfObj[4];
tmp.pdfd = pdfObj[13];
pdfObj = tmp;
tmp = null;
jirislav
  • 340
  • 6
  • 16