javascript:为数组中的每个字符串创建自动子字符串

标签 javascript arrays

是否可以在 javascript 中为数组中的每个字符串创建自动子字符串? 例如:

var test = 
[
 "banana",
 "apple",
 "pear",
 "cologne"
];

var conclusion = prompt("choose one")

// something that would get the following result:

if( conclusion === banana)

{

print("b");

print("ba");

print ("ban");

// ...

print("banana");

// and the same for the rest of the strings in the Array.

最佳答案

这是一个简短的解决方案,我让你自己理解作为作业:

var test ="banana";
test.split("").forEach((x,i,arr) => console.log(arr.slice(0,i+1).join("")));

关于javascript:为数组中的每个字符串创建自动子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41088286/

相关文章:

javascript - 从 Controller 传递数据时,Jquery Grid 绑定(bind)需要时间

javascript - 检查字符串是否在列表中(JavaScript)

php - 将数组中的数据保存在变量中-Moodle

c# - 字节数组的内容

java - 读取前 100 个字符串数组,java i/o

php - 将 PHP 数组回显为 HTML?

javascript - 限制可写入路径的记录数(引用安全规则中的其他路径)

javascript - 当网站内容溢出时,无法向上滚动?

javascript - AJAX 调用重新加载仅有效一次

javascript - 在 Angular 应用程序中使用复选框输入填充嵌套数组