I\'m trying to iterate over a \"value\" list and convert it into a string. Here is the code:
var blkstr = $.each(value, function(idx2,val2) {
Array.prototype.toString()
The toString() method returns a string representing the specified array and its elements.
var months = ["Jan", "Feb", "Mar", "Apr"];
months.toString(); // "Jan,Feb,Mar,Apr"
Syntax
arr.toString()
Return value
A string representing the elements of the array.
for more information :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
jQuery.each
is just looping over the array, it doesn't do anything with the return value∆. You are looking for jQuery.map (I also think that get()
is unnecessary as you are not dealing with jQuery objects):
var blkstr = $.map(value, function(val,index) {
var str = index + ":" + val;
return str;
}).join(", ");
DEMO
But why use jQuery at all in this case? map
only introduces an unnecessary function call per element.
var values = [];
for(var i = 0, l = value.length; i < l; i++) {
values.push(i + ':' + value[i]);
}
// or if you actually have an object:
for(var id in value) {
if(value.hasOwnProperty(id)) {
values.push(id + ':' + value[id]);
}
}
var blkstr = values.join(', ');
∆: It only uses the return value whether it should continue to loop over the elements or not. Returning a "falsy" value will stop the loop.
I needed an array to became a String rappresentation of an array I mean I needed that
var a = ['a','b','c'];
//became a "real" array string-like to pass on query params so was easy to do:
JSON.stringify(a); //-->"['a','b','c']"
maybe someone need it :)
convert an array to a GET param string that can be appended to a url could be done as follows
function encodeGet(array){
return getParams = $.map(array , function(val,index) {
var str = index + "=" + escape(val);
return str;
}).join("&");
}
call this function as
var getStr = encodeGet({
search: $('input[name="search"]').val(),
location: $('input[name="location"]').val(),
dod: $('input[name="dod"]').val(),
type: $('input[name="type"]').val()
});
window.location = '/site/search?'+getStr;
which will forward the user to the /site/search? page with the get params outlined in the array given to encodeGet.
not sure if this is what you wanted but
var arr = [A, B, C];
var arrString = arr.join(", ");
This results in the following output:
A, B, C
Four methods to convert an array to a string.
Coercing to a string
var arr = ['a', 'b', 'c'] + []; // "a,b,c"
var arr = ['a', 'b', 'c'] + ''; // "a,b,c"
Calling .toString()
var arr = ['a', 'b', 'c'].toString(); // "a,b,c"
Explicitly joining using .join()
var arr = ['a', 'b', 'c'].join(); // "a,b,c" (Defaults to ',' seperator)
var arr = ['a', 'b', 'c'].join(','); // "a,b,c"
You can use other separators, for example, ', '
var arr = ['a', 'b', 'c'].join(', '); // "a, b, c"
Using JSON.stringify()
This is cleaner, as it quotes strings inside of the array and handles nested arrays properly.
var arr = JSON.stringify(['a', 'b', 'c']); // '["a","b","c"]'