当前位置:网站首页>In JS, string and array are converted to each other (II) -- the method of converting array into string
In JS, string and array are converted to each other (II) -- the method of converting array into string
2022-07-06 21:10:00 【viceen】
js in , String and array conversion ( Two )—— The method of converting an array into a string
Method 1、toString() Method
1、 function : You can convert each element into a string , Then connect the output with commas and display it .
2、 Usage method :
var arr = [0,1,2,3]; // Define an array
var str = arr.toString(); // hold arr Array utilization toString() Convert to string
console.log(str); // Input string '0,1,2,3'
When the array is in a string environment ,js Automatically called toString() Method to convert an array into a string .
var arr = [0,1,2,3]; // Define an array
var arr1 = [4,5,6,7]; // Define an array
var str = arr + arr1; // Array join operation
console.log(str); // return '0,1,2,34,5,6,7'
var arr = [0,1,2,3]; // Define an array
var arr1 = [4,5,6,7]; // Define an array
var str = arr + ',' + arr1; // Array join operation
console.log(str); // return '0,1,2,3,4,5,6,7'
toString() When converting an array to a string , First, convert each element of the array into a string . When each element is converted to a string , Use commas to separate , Output these strings as a list .
var arr = [[1,[2,3],[4,5]],[6,[7,[8,9],0]]]; // Define multidimensional arrays
var str = arr.toString(); // Convert an array to a string
console.log(str); // Return string '1,2,3,4,5,6,7,8,9,0'
Where array arr It's a multidimensional array ,JavaScript Will call... Iteratively toString() Method converts all arrays to strings .
Method 2、toLocalString() Method
1、 function : And toString() The method usage is basically the same , The difference lies in toLocalString() Method can concatenate the generated string with a user's locale specific delimiter , Form a string .
var array = [1,2,3,4,5]; // Define an array
var str = array.toLocaleString(); // Convert an array to a local string
console.log(str); // Return string '1,2,3,4,5'
- According to Chinese usage habits , First convert the number to a floating-point number, and then perform the string conversion operation
Method 3、join() Method
1、 function : Convert an array to a string , However, it can specify the delimiter , If Omit parameters , By default comma As a separator
var arr = [1,2,3]; // Define an array
var str = arr.join("-"); // Specify the separator -
console.log(str); // Return string '1-2-3'
var arr = [1,2,3]; // Define an array
var str = arr.join(","); // Specify the separator
console.log(str); // Return string '1,2,3'
边栏推荐
- Reference frame generation based on deep learning
- 966 minimum path sum
- Aike AI frontier promotion (7.6)
- How to turn a multi digit number into a digital list
- 【mysql】游标的基本使用
- 2017 8th Blue Bridge Cup group a provincial tournament
- Aiko ai Frontier promotion (7.6)
- Performance test process and plan
- Reinforcement learning - learning notes 5 | alphago
- Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
猜你喜欢
OneNote in-depth evaluation: using resources, plug-ins, templates
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
20220211 failure - maximum amount of data supported by mongodb
Deployment of external server area and dual machine hot standby of firewall Foundation
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
【微信小程序】運行機制和更新機制
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
Reinforcement learning - learning notes 5 | alphago
ICML 2022 | flowformer: task generic linear complexity transformer
Manifest of SAP ui5 framework json
随机推荐
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
Vim 基本配置和经常使用的命令
What key progress has been made in deep learning in 2021?
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
None of the strongest kings in the monitoring industry!
Spiral square PTA
如何实现常见框架
How do I remove duplicates from the list- How to remove duplicates from a list?
967- letter combination of telephone number
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
OSPF multi zone configuration
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
[200 opencv routines] 220 Mosaic the image
PG基础篇--逻辑结构管理(事务)
Aiko ai Frontier promotion (7.6)
OAI 5g nr+usrp b210 installation and construction
代理和反向代理
Performance test process and plan
Math symbols in lists