将JavaScript数组转换为逗号分隔列表的简便方法? [英] Easy way to turn JavaScript array into comma-separated list?

查看:81
本文介绍了将JavaScript数组转换为逗号分隔列表的简便方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中有一个一维的字符串数组,我想把它变成以逗号分隔的列表。在花园种类的JavaScript(或jQuery)中有一种简单的方法可以将其转换为以逗号分隔的列表吗? (我知道如何迭代数组并通过连接自己构建字符串,如果这是唯一的方法。)

I have a one-dimensional array of strings in JavaScript that I'd like to turn into a comma-separated list. Is there a simple way in garden-variety JavaScript (or jQuery) to turn that into a comma-separated list? (I know how to iterate through the array and build the string myself by concatenation if that's the only way.)

推荐答案

< a href =https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join =noreferrer> Array.prototype.join()方法:

var arr = ["Zero", "One", "Two"];

document.write(arr.join(", "));

这篇关于将JavaScript数组转换为逗号分隔列表的简便方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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