简体   繁体   English

为什么在使用 split function of javascript 时出现错误?

[英]Why am I getting an error when using split function of javascript?

I'm trying to split my string but not get proper output as per my expected.我正在尝试拆分我的字符串,但没有按照我的预期获得正确的 output。

Input输入

let name = "helloguys";

Output Output

h
he
hel
hell
hello
hellog
hellogu
helloguy
helloguys
let name = "mynameisprogrammer";
for (let index = 1; index <= name.length; index++) {
  let finalData = name.split(name[index]);
  console.log(finalData[0]);
}

enter image description here在此处输入图像描述

You can use substring to take parts of your input:您可以使用substring来获取部分输入:

 let name = "mynameisprogrammer"; for (let index = 1; index <= name.length; index++) { let finalData = name.substring(0, index); console.log(finalData); }

You could use Array.map() along with String.slice() to split the string into the required values:您可以使用Array.map()String.slice()将字符串拆分为所需的值:

 let name = "mynameisprogrammer"; let result = [...name].map((chr, idx) => name.slice(0, idx + 1)); console.log('Result:', result)
 .as-console-wrapper { max-height: 100%;important; }

If you need this to be unicode aware, you might want to avoid using string substring / slice etc.如果你需要它是 unicode 感知的,你可能想避免使用字符串 substring / slice 等。

Instead convert to array, and split there.而是转换为数组,然后在那里拆分。

eg..例如..

 let name = "mynameisprogrammer"; const nameA = [...name]; for (let ix = 1; ix <= nameA.length; ix += 1) console.log(nameA.slice(0, ix).join(''));
 .as-console-wrapper { max-height: 100%;important; }

ps.附言。 To see what I mean by unicode aware, try using the name from the code above into some of the other answers.要了解我所说的 unicode aware 是什么意思,请尝试将上面代码中的名称用于其他一些答案。

You can simply use a variable and keeping add value of current index and keep printing on each iteration, you don't need split here您可以简单地使用一个变量并保持当前索引的附加值并在每次迭代中保持打印,您不需要在此处拆分

 let name = "helloguys"; function printName(name) { let temp = '' for (let index = 0; index < name.length; index++) { temp += name[index] console.log(temp); } } printName(name)

This is how you could do it.这就是你可以做到的。 I'm not sure if you wanted them to be object otherwise you could use string.substring我不确定你是否希望它们是 object 否则你可以使用string.substring

 let name = "mynameisprogrammer"; for (let index = 0; index < name.length; index++) { let finalData = name.split(""); console.log(finalData.slice(0,index+1).join("")); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 Javascript 中使用“类”时会出现语法错误 - Why am I getting a Syntax error when using “class” in Javascript Javascript-为什么在AngularJS中使用ng-if时出现此错误? - Javascript - Why I am getting this error when I am using ng-if in AngularJS? 我在使用拆分方法时遇到错误Uncaught ReferenceError:函数未定义 - getting error when I am using split method Uncaught ReferenceError: function is not defined 为什么我在下面的 javaScript function 中没有出现类型错误? - Why am i not getting a type error in the below javaScript function? Javascript - 为什么我的功能出现NAN错误 - Javascript - Why am I Getting NAN error with my function JavaScript,为什么会出现错误“函数add中未定义数组” - JavaScript, why am I getting the error “array is not defined in the function add” 使用以下 function 将 GEE javascript 转换为 Python 脚本时出现错误 - Using the below function I am getting an error when I convert the GEE javascript to Python script 尝试在Colfusion中使用JavaScript将src属性添加到img标签时,为什么会出现错误? - Why am I getting an error when trying to add the src attribute to an img tag using JavaScript within Colfusion? 为什么我在使用 javascript 数组中的 .length 属性时收到此错误? - Why I am getting this error using the .length property in javascript array? 在不同的页面上使用相同的 JavaScript,为什么会出现错误? - Using the same JavaScript on a different page, why am I getting an error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM