b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

使用JS在字符串和字符串数组之间转换

电脑杂谈  发布时间:2020-05-18 05:18:59  来源:网络整理

js 数组转json数组_js 字符串 数组_js字符数组转字符串

在JavaScript中js 字符串 数组,可以使用split()和Array.from()方法将字符串转换为字符串数组.

split()方法用于将给定的字符串拆分为字符串数组. 此方法使用参数中提供的指定分隔符将其拆分为子字符串.

js字符数组转字符串_js 数组转json数组_js 字符串 数组

语法:

str.split(separator, limit)

js 字符串 数组_js字符数组转字符串_js 数组转json数组

例如:

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<p id="str">Welcome to here!</p>
		<p id="arr"></p>
		<script>
			var str =document.getElementById("str").innerHTML;
			document.getElementById("arr").innerHTML = str.split("");
		</script>
	</body>
</html>

js 数组转json数组_js 字符串 数组_js字符数组转字符串

结果:

Welcome to here!
W,e,l,c,o,m,e, ,t,o, ,h,e,r,e,!

js 字符串 数组_js 数组转json数组_js字符数组转字符串

Array.from()方法是javascript中的内置函数js 字符串 数组,可从给定数组创建新的数组实例. 对于字符串,字符串的每个字母将转换为新数组实例的元素;对于整数值,简单的新数组实例将获取给定数组的元素.

语法:

Array.from(str)

例如:

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<p id="str">Welcome to here!</p>
		<p id="arr"></p>
		<script>
			var str = document.getElementById("str").innerHTML; 
            document.getElementById("arr").innerHTML = Array.from(str); 
		</script>
	</body>
</html>

输出结果:


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-213920-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...