javascript - Convert Object Array to an Object -


i have returned object list: data.d[15]

and 1 sample of it:

data.d[3] = { cityid: 2, cityname: "ankara"} 

i want convert 1 object as

cities{ 1: "istanbul", 2: "ankara", 3: "new york" } 

and should generic, dont know "cityid" , "cityname" field names. best method it?

thank all... have send fieldnames field object -no dependencies important code-, has been resolved.

            var url = this.options.url + "/" + field.values,                 id = field.fieldid,                 title = field.fieldtitle;              this.getjson(url, {}, function (rdata) {                 var obj = {};                  (i = 0; < rdata.d.length; i++)                     obj[rdata.d[i][id]] = rdata.d[i][title];                  $("#" + parentid).html(self.gethtmlofformdata(type, obj));             }); 

maybe need detect property contains name of city. maybe can work?

var idprop, nameprop; (var prop in data.d[0]) {     if (typeof data.d[0][prop] === "string") nameprop = prop;     if (typeof data.d[0][prop] === "number") idprop = prop; }  var cities = {}; (var = 0; < data.d.length; i++)    cities[data.d[i][idprop]] = data.d[i][nameprop]; 

keep in mind works if:

  1. data.d isn't empty array;
  2. there's 1 string property contains city name;
  3. there's 1 numeric property contains city id.

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -