迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中将字符串转换为数组

作者:迹忆客 最近更新:2023/03/10 浏览次数:

我们将介绍 JavaScript 中将字符串转换为数组的三种方法。我们将转换如何将字符串转换为数值数组以及转换为字符串数组。

JSON.parse() 表达式用于将从 Web 服务器接收到的数据解析为对象和数组。如果接收到的数据是 JSON 对象的形式,它会将其转换为 JavaScript 对象。而且,如果数据是从数组派生的 JSON 值,JSON.parse() 会将数据转换为 JavaScript 数组。我们可以使用 JSON.parse() 将逗号分隔的数字字符串转换为数组。我们可以将括号与字符串连接起来,将其转换为数字数组。

例如,创建一个变量 data 并将值 0,1,2,3 作为字符串存储在变量中。在 data 变量上使用 JSON.parse() 并连接变量前后的左括号和右括号。将表达式存储在变量 arr 中。然后,在控制台中记录变量。此外,在控制台中记录 typeof 数组的第一个元素。

在下面的示例中,我们将一串数字转换为一组数字。我们可以使用 typeof 关键字知道数组项的类型。输出部分显示数组元素属于 number 类型。

示例代码:

var data = "0,1,2,3";
var arr = JSON.parse("[" + data + "]");
console.log( arr)
console.log(typeof arr[0])

输出:

(4) [0, 1, 2, 3]
number

Array.from() 方法允许你从特定长度的字符串创建数组。我们可以将需要转换的字符串传入方法中,方法将其转换为字符串数组。但是,如果我们有 .,这种方法没有帮助。在字符串之间,因为它还会将 , 添加到字符串数组中。

例如,创建一个变量 num 并将值 1234 作为字符串存储在变量中。使用对象 Array 调用 from() 方法并提供 num 变量作为函数中的参数。将表达式存储在变量 arr 中。然后,使用 console.log() 在控制台中显示变量。此外,在控制台中记录 typeof 数组的第一个元素。

下面的示例将数字字符串转换为字符串数组。字符串的每个字符都被转换为数组的字符串元素。输出部分显示数组元素是 string 类型。

示例代码:

var num = "1234";
var arr = Array.from(data);
console.log(arr)
console.log(typeof arr[0])

输出:

(4) ["1", "2", "3", "4"]
string

展开运算符由三个点表示,后跟变量 ...data。它是一个广泛使用的 ES6 函数。此方法将字符串转换为字符串数组。但是,如果我们在字符串之间使用逗号,则此方法没有用,因为它还会将 , 添加到字符串数组中。我们可以传递包含大括号内字符串的变量。在变量之前,我们可以添加三个点。即 [...data]

例如,将值 1234 作为字符串存储在变量 data 中。将变量 data 写入括号内并在其前添加三个点。将此表达式存储在变量 arr 中。然后,在控制台中记录变量。此外,在数组的第一个元素之前使用 typeof 关键字并将其登录到控制台。

在示例中,我们将一串数字转换为一个字符串数组,就像第二种方法一样。但是,这一次,我们使用了扩展运算符。输出部分显示数组元素是 string 类型。

示例代码:

var data = "1234";
var arr = [...data];
console.log(arr)
console.log(typeof arr[0])

输出:

(4) ["1", "2", "3", "4"]
string

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便