0
var Data = {    "1":"United States",
                "2":"United Kingdom",
                "3":"Aruba",
                "4":"United Kingdom",
                "5":"Aruba",
                "6":"Bahrain",
                "7":"United Kingdom",
                "8":"Algeria",
                "9":"Andorra",
                "10":"American Samoa"
}
var IdKeyValuePair = [{"DropD1":Data},{"DropD2":Data},{"DropD3":Data},{"DropD4":Data}];
var obj= {
            id           : IdKeyValuePair,
            MultiColuumn : true,
            Checkbox     : true,
            Pagination   : false,
            max_height   : 400

         };

I want to print the name of DropD1, DropD2.. i am tryin in such a manner. but i am unable to do this..

var idName = [],i;
idLen = obj.id.length;
for(i=0;i<idLen;i++){
    idName[i] = ''+obj.id[i];
    console.log(idName[i]);
}

i am getting the DropD1, DropD2.. but the problem is that it in a object form so how can i convert it in a string... i want a result is such format...

for(i=0;i<idLen;i++){
   console.log(idName[i]);
}

Expected Output in string format not in object:

DropD1 DropD2 DropD3

coder
  • 1,874
  • 2
  • 22
  • 31
  • possible duplicate of [How can i find the length of this Json object](http://stackoverflow.com/questions/14272416/how-can-i-find-the-length-of-this-json-object) - consider reading answers to your previous question as all of them show exactly how to iterate properties of an object. And while you are at it - comment/accept answers there. – Alexei Levenkov Jan 11 '13 at 07:49
  • http://stackoverflow.com/questions/1876485/get-property-names-in-json-objects – Moritz Petersen Jan 11 '13 at 07:52

4 Answers4

0

try this. It's working to me

var IdKeyValuePair = [{ "DropD1": Data["1"] }, { "DropD2": Data["2"] }, { "DropD3": Data["3"]}, { "DropD4": Data["4"]}]; so on and so forth.
Jobert Enamno
  • 4,403
  • 8
  • 41
  • 63
0

I got one solution using browser stringify property ::

var jsonobj={"name":"George", "age":29, "friends":["John", "Sarah", "Albert"]}
var jsonstr=JSON.stringify(jsonobj)
alert(typeof jsonstr) //string

U can convert it to javascript object in following way ::

var jsonstr='{"name":"George", "age":29, "friends":["John", "Sarah", "Albert"]}'
var george=JSON.parse(jsonstr) //convert JSON string into object
alert(george.age)

For more info about json refer following link,

http://www.javascriptkit.com/jsref/json.shtml

Prateek
  • 12,014
  • 12
  • 60
  • 81
0

Try this

var idName = [],i;
idLen = obj.id.length;
for(i=0;i<idLen;i++){
    idName[i] = obj.id[i];
}
console.log(idName.join(" "));
stanke
  • 276
  • 2
  • 13
0

Try this:

  var Data = {    "1":"United States",
                  "2":"United Kingdom",
                  "3":"Aruba",
                  "4":"United Kingdom",
                  "5":"Aruba",
                  "6":"Bahrain",
                  "7":"United Kingdom",
                  "8":"Algeria",
                  "9":"Andorra",
                  "10":"American Samoa"
  }
  var IdKeyValuePair = [{"DropD1":Data},{"DropD2":Data},{"DropD3":Data},{"DropD4":Data}];
  var obj= {
              id           : IdKeyValuePair,
              MultiColuumn : true,
              Checkbox     : true,
              Pagination   : false,
              max_height   : 400

           };

  var idName = [],i;
  idLen = obj.id.length;
  for(i=0;i<idLen;i++){
    idName[i] = obj.id[i];
    console.info(JSON.stringify(idName[i]));
  }

You were unnecessarily converting the object to string by adding empty string to the object ("" + object)

Munish Poonia
  • 808
  • 5
  • 4