Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

I was trying to convert a list into a string by iterating into it using the below code:

var temp = $.each(value, function(idx2,val2) {                    

     var str = idx2 + ":" + val2;

     alert(str);

     return str;

}).get().join(", "); 

Can anyone help me what is wrong in this code?

1 Answer

0 votes
by (26.7k points)

Basically, if you have added the value as an associative array, then this code would works:

var value = { "aaa": "111", "bbb": "222", "ccc": "333" };

var temp = [];

$.each(value, function(idx2,val2) {                    

  var str = idx2 + ":" + val2;

  temp.push(str);

});

console.log(temp.join(", "));

Also, each() function can only help you to iterate over an array.

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java expert? join Java Certification now!!

Related questions

0 votes
1 answer
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...