将javascript数组转换为字符串 [英] Convert javascript array to string
本文介绍了将javascript数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遍历值"列表并将其转换为字符串.代码如下:
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) {
var str = idx2 + ":" + val2;
alert(str);
return str;
}).get().join(", ");
alert() 函数工作正常并显示正确的值.但不知何故,jquery 的 .get() 函数没有获得正确的对象类型并且失败了.我做错了什么?
alert() function works just fine and displays the proper value. But somehow, jquery's .get() function doesn't get the right sort of object and fails. What am I doing wrong?
推荐答案
如果 value
不是普通数组,这样的代码可以正常工作:
If value
is not a plain array, such code will work fine:
var value = { "aaa": "111", "bbb": "222", "ccc": "333" };
var blkstr = [];
$.each(value, function(idx2,val2) {
var str = idx2 + ":" + val2;
blkstr.push(str);
});
console.log(blkstr.join(", "));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
(输出将出现在开发控制台中)
(output will appear in the dev console)
正如 Felix 所说,each()
只是迭代数组,仅此而已.
As Felix mentioned, each()
is just iterating the array, nothing more.
这篇关于将javascript数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文