当前位置:网站首页>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'
边栏推荐
- ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
- Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
- Nodejs教程之Expressjs一篇文章快速入门
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- 愛可可AI前沿推介(7.6)
- Data Lake (VIII): Iceberg data storage format
- Huawei device command
- Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
- 【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
猜你喜欢
Spark SQL chasing Wife Series (initial understanding)
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
Opencv learning example code 3.2.3 image binarization
Deployment of external server area and dual machine hot standby of firewall Foundation
Aike AI frontier promotion (7.6)
2017 8th Blue Bridge Cup group a provincial tournament
数据湖(八):Iceberg数据存储格式
##无yum源安装spug监控
防火墙基础之外网服务器区部署和双机热备
随机推荐
[200 opencv routines] 220 Mosaic the image
el-table表格——sortable排序 & 出现小数、%时排序错乱
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
How do I remove duplicates from the list- How to remove duplicates from a list?
正则表达式收集
038. (2.7) less anxiety
[MySQL] basic use of cursor
Dynamically switch data sources
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
Reflection operation exercise
c#使用oracle存储过程获取结果集实例
【论文解读】用于白内障分级/分类的机器学习技术
Xcode6 error: "no matching provisioning profiles found for application"
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
The biggest pain point of traffic management - the resource utilization rate cannot go up
Nodejs tutorial let's create your first expressjs application with typescript
华为设备命令