将javascript数组转换为字符串 [英] Convert javascript array to string

查看:106
本文介绍了将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 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所说的,each()只是迭代数组,仅此而已.

As Felix mentioned, each() is just iterating the array, nothing more.

这篇关于将javascript数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆