当前位置:网站首页>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'
边栏推荐
- 什么是RDB和AOF
- 2017 8th Blue Bridge Cup group a provincial tournament
- 2110 summary of knowledge points and common problems in redis class
- use. Net drives the OLED display of Jetson nano
- OAI 5g nr+usrp b210 installation and construction
- The mail command is used in combination with the pipeline command statement
- Huawei device command
- Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
- 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
- 拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
猜你喜欢

基于STM32单片机设计的红外测温仪(带人脸检测)

2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性

Implementation of packaging video into MP4 format and storing it in TF Card
![[weekly pit] calculate the sum of primes within 100 + [answer] output triangle](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle

Use of OLED screen

KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐

Manifest of SAP ui5 framework json

Common doubts about the introduction of APS by enterprises

Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)

The mail command is used in combination with the pipeline command statement
随机推荐
Minimum cut edge set of undirected graph
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
[DIY]自己设计微软MakeCode街机,官方开源软硬件
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
##无yum源安装spug监控
2022 portal crane driver registration examination and portal crane driver examination materials
面试官:Redis中有序集合的内部实现方式是什么?
【mysql】触发器
【微信小程序】運行機制和更新機制
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
Xcode6 error: "no matching provisioning profiles found for application"
[diy] self designed Microsoft makecode arcade, official open source software and hardware
It's almost the new year, and my heart is lazy
Data Lake (VIII): Iceberg data storage format
Leetcode hot topic Hot 100 day 32: "minimum coverage substring"
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
使用.Net驱动Jetson Nano的OLED显示屏
C # use Oracle stored procedure to obtain result set instance