當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
javascript中的字符串和数组的互转
生活随笔
收集整理的這篇文章主要介紹了
javascript中的字符串和数组的互转
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
javascript中的數(shù)組轉(zhuǎn)換成字符串用toString(),或者用join()。
將數(shù)組和字符串用+連接,賦給一個變量,這個變量自動轉(zhuǎn)變成字符串了。
?
字符串轉(zhuǎn)換成數(shù)組用split(',')
<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>js中數(shù)組和字符串的互轉(zhuǎn)</title> </head><body><script>var msgsnew = ["test", "test", "test", "test", "test"];//var tstr=msgsnew.join() var tstr = msgsnew.join(','); //數(shù)組轉(zhuǎn)字符串 console.log("tstr", tstr);var tstrtwo = msgsnew.toString(); //數(shù)組轉(zhuǎn)字符串var tstrthree = "可以轉(zhuǎn)字符串的" + msgsnew //js中的+ console.log("+后轉(zhuǎn)變成字符串的", tstrthree, typeof tstrthree);console.log("數(shù)組:" + msgsnew + ":" + (msgsnew instanceof Array), "數(shù)組轉(zhuǎn)字符串:" + tstrtwo + ":" + (tstrtwo instanceof Array));console.log(msgsnew);var tarra = tstr.split(','); //字符串轉(zhuǎn)數(shù)組 console.log(msgsnew instanceof Array, typeof tstr, tarra);</script></body></html>?
轉(zhuǎn)載于:https://www.cnblogs.com/zhumeiming/p/9803772.html
總結
以上是生活随笔為你收集整理的javascript中的字符串和数组的互转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu下局域网内+花生壳远程调试程
- 下一篇: gradle idea java ssm