当前位置:网站首页>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'
边栏推荐
- 审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
- Xcode6 error: "no matching provisioning profiles found for application"
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
- Nodejs tutorial let's create your first expressjs application with typescript
- ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
- 性能测试过程和计划
- Nodejs tutorial expressjs article quick start
- 7. Data permission annotation
- 面试官:Redis中有序集合的内部实现方式是什么?
- 2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
猜你喜欢

拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条

嵌入式开发的7大原罪

Variable star --- article module (1)

【论文解读】用于白内障分级/分类的机器学习技术

039. (2.8) thoughts in the ward

Aiko ai Frontier promotion (7.6)

Spark SQL chasing Wife Series (initial understanding)

3D人脸重建:从基础知识到识别/重建方法!

Is it profitable to host an Olympic Games?

【mysql】触发器
随机推荐
Is this the feeling of being spoiled by bytes?
Swagger UI教程 API 文档神器
基于STM32单片机设计的红外测温仪(带人脸检测)
Is it profitable to host an Olympic Games?
分糖果
PHP saves session data to MySQL database
Study notes of grain Mall - phase I: Project Introduction
[wechat applet] operation mechanism and update mechanism
967- letter combination of telephone number
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
3D人脸重建:从基础知识到识别/重建方法!
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
This year, Jianzhi Tencent
Web开发小妙招:巧用ThreadLocal规避层层传值
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
Interviewer: what is the internal implementation of ordered collection in redis?
Web开发小妙招:巧用ThreadLocal规避层层传值
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
968 edit distance
OneNote 深度评测:使用资源、插件、模版