当前位置:网站首页>js中,字符串和数组互转(二)——数组转为字符串的方法
js中,字符串和数组互转(二)——数组转为字符串的方法
2022-07-06 12:51:00 【viceen】
js中,字符串和数组互转(二)——数组转为字符串的方法
方法1、toString()方法
1、功能:可以把每个元素转换为字符串,然后以逗号连接输出并显示。
2、使用方法:
var arr = [0,1,2,3]; //定义一个数组
var str = arr.toString(); //把arr数组利用toString()转换为字符串
console.log(str); //输入字符串'0,1,2,3'
当数组处于字符串环境中时,js会自动调用 toString() 方法将数组转换成字符串。
var arr = [0,1,2,3]; // 定义数组
var arr1 = [4,5,6,7]; // 定义数组
var str = arr + arr1; // 数组连接操作
console.log(str); // 返回'0,1,2,34,5,6,7'
var arr = [0,1,2,3]; // 定义数组
var arr1 = [4,5,6,7]; // 定义数组
var str = arr + ',' + arr1; // 数组连接操作
console.log(str); // 返回'0,1,2,3,4,5,6,7'
toString() 在把数组转换成字符串时,首先要将数组的每个元素都转换为字符串。当每个元素都被转换为字符串时,才使用逗号进行分隔,以列表的形式输出这些字符串。
var arr = [[1,[2,3],[4,5]],[6,[7,[8,9],0]]]; // 定义多维数组
var str = arr.toString(); // 把数组转换为字符串
console.log(str); // 返回字符串'1,2,3,4,5,6,7,8,9,0'
其中数组 arr 是一个多维数组,JavaScript 会以迭代的方式调用 toString() 方法把所有数组都转换为字符串。
方法2、toLocalString()方法
1、功能: 与 toString() 方法用法基本相同,区别在于 toLocalString() 方法能够使用用户所在地区特定的分隔符把生成的字符串连接起来,形成一个字符串。
var array = [1,2,3,4,5]; // 定义数组
var str = array.toLocaleString(); // 把数组转换为本地字符串
console.log(str); // 返回字符串'1,2,3,4,5'
- 根据中国的使用习惯,先把数字转换为浮点数之后再执行字符串转换操作
方法3、join() 方法
1、功能: 把数组转换为字符串,不过它可以指定分隔符,如果省略参数,默认使用逗号作为分隔符
var arr = [1,2,3]; //定义数组
var str = arr.join("-"); //指定分隔符-
console.log(str); //返回字符串'1-2-3'
var arr = [1,2,3]; // 定义数组
var str = arr.join(","); // 指定分隔符
console.log(str); // 返回字符串'1,2,3'
边栏推荐
- 华为设备命令
- Performance test process and plan
- Core principles of video games
- 【mysql】游标的基本使用
- 过程化sql在定义变量上与c语言中的变量定义有什么区别
- Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
- Xcode6 error: "no matching provisioning profiles found for application"
- Summary of different configurations of PHP Xdebug 3 and xdebug2
- document.write()的用法-写入文本——修改样式、位置控制
- OneNote 深度评测:使用资源、插件、模版
猜你喜欢
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
[MySQL] basic use of cursor
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
【论文解读】用于白内障分级/分类的机器学习技术
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
968 edit distance
防火墙基础之外网服务器区部署和双机热备
Core principles of video games
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
随机推荐
C # use Oracle stored procedure to obtain result set instance
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
7. Data permission annotation
知识图谱之实体对齐二
Opencv learning example code 3.2.3 image binarization
R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
[wechat applet] operation mechanism and update mechanism
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
快过年了,心也懒了
OAI 5g nr+usrp b210 installation and construction
Use of OLED screen
Dynamically switch data sources
Reference frame generation based on deep learning
Taylor series fast Fourier transform (FFT)
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
数据湖(八):Iceberg数据存储格式
防火墙基础之外网服务器区部署和双机热备
Interviewer: what is the internal implementation of ordered collection in redis?