javascript - 将 'prompt()' 的结果拆分为数组中的项目?

标签 javascript arrays prompt

问题

如何仅使用一个 prompt(); 将多个值添加到列表中?

代码

let items = [];
action = prompt("Enter");

如果我的输入是 Hello World!,那么我怎样才能使我的列表看起来像这样:

items = ["Hello", "World!"];

尝试

这是我能得到的最接近的结果(它失败了,因为我只能使用一个 prompt();):

let first = prompt("Enter 1");
let second = prompt("Enter 2");
items.push(first);
items.push(second);

最佳答案

您可以拆分接收到的字符串以获得具有两个单独值的数组

let action = prompt();
let items = action.split(' ');

console.log(items);

关于javascript - 将 'prompt()' 的结果拆分为数组中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253565/

相关文章:

javascript - 流 : Cannot get . .. 因为对象文字中缺少属性 ...

javascript - Google Sheet 脚本有条件地将空白单元格的值设置为今天的日期,但保留现有数据相同

javascript - body 加载事件

javascript - 集成 angularjs 和 d3,简单的数据绑定(bind)示例

javascript - Cypress - 是否可以在提示中验证文本?

c# - iTextSharp 生成的 PDF : How to send the pdf to the client and add a prompt?

javascript - 带提示的两个数字的总和

C 程序在创建数组和写入 .txt 文件时永远占用

c# - 字符串数组。包含?

java - 更改数组值java