jQuery:将javascript数组转换为字符串 [英] jQuery : Convert javascript array to string
本文介绍了jQuery:将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?
推荐答案
如果值
是关联数组,这样的代码可以正常工作:
If value
is associative 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所提到的,每个()
只是迭代数组,仅此而已。
As Felix mentioned, each()
is just iterating the array, nothing more.
这篇关于jQuery:将javascript数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文