数组tostring方法_数组toString()方法以及JavaScript中的示例

数组tostring方法

JavaScript toString()方法 (JavaScript toString() method)

toString() method is used to convert an array to the string. It is called with the array name and returns the string containing array elements with comma separated.

toString()方法用于将数组转换为字符串。 使用数组名称调用它,并返回包含以逗号分隔的数组元素的字符串。

Syntax:

句法:

    array.toString();

Parameters: None

参数:

Return value: A string containing array elements

返回值:包含数组元素的字符串

Example:

例:

    Input:
    var arr = [10, 20, 30, 40, 50];

    Function call:
    var str = arr.toString();    

    Output:
    str = "10,20,30,40,50"

JavaScript code to convert array’s elements to the string using Array.toString() method

JavaScript代码使用Array.toString()方法将数组的元素转换为字符串

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		var arr1 = ["Manju", "Amit", "Abhi", "Radib"];
		var arr2 = [10, 20, 30, 40, 50];
		var arr3 = [-10, -20, 0, 10, 20];
		
		var str1 = arr1.toString();
		var str2 = arr2.toString();
		var str3 = arr3.toString();
		
		document.write("str1: " + str1 + "<br>");
		document.write("str2: " + str2 + "<br>");
		document.write("str3: " + str3 + "<br>");
		
		document.write("printing the types of the objects...<br>");
		document.write("type of arr1: " + typeof(arr1) + "<br>");
		document.write("type of arr2: " + typeof(arr2) + "<br>");
		document.write("type of arr3: " + typeof(arr3) + "<br>");
		document.write("type of str1: " + typeof(str1) + "<br>");
		document.write("type of str2: " + typeof(str2) + "<br>");
		document.write("type of str3: " + typeof(str3) + "<br>");
	</script>
</body>
</html>

Output

输出量

str1: Manju,Amit,Abhi,Radib
str2: 10,20,30,40,50
str3: -10,-20,0,10,20
printing the types of the objects...
type of arr1: object
type of arr2: object
type of arr3: object
type of str1: string
type of str2: string
type of str3: string


翻译自: https://www.includehelp.com/code-snippets/array-toString-method-with-example-in-javascript.aspx

数组tostring方法

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值