简体   繁体   English

'TypeError: name.push is not a function' 同时将字符串推入空数组

[英]'TypeError: name.push is not a function' while pushing a string into an empty array

I have a multi line string which I am trying to extract some information by splitting the string and push to some empty arrays.我有一个多行字符串,我试图通过拆分字符串来提取一些信息并推送到一些空的 arrays。 But I am having some type error while trying to push array.但是我在尝试推送数组时遇到了一些type error My code as below.我的代码如下。 I did some research in stack overflow and some suggestion is to put the array [] as dictionary {} .我对堆栈溢出做了一些研究,一些建议是将数组[]作为字典{} But I don't want any dictionary.但我不想要任何字典。 Can someone help?有人可以帮忙吗?

 < script > var str = `PP 0800 / 00 / XX: Units: 2: Total: 4.70 || PP 0800 / 00 / XX: Units: 1: Total: 2.35 ||` //This is the multi line string var singleLine = str.replace(/(\r\n|\n|\r)/gm, ""); //Converting multi line string into a single line var splittedArray = singleLine.split("||"); //splitting the string based on the symbol || var name = []; //Initializing three empty arrays var unit = []; var total = []; for (i = 0; i < splittedArray.length - 1; i++) { //Looping through the splittedArray var furtherSplit = splittedArray[i].split(":"); //Splitting each array further based on the symbol: name.push(furtherSplit[0]); //This is where name, unit and total pushing into three separate arrays unit.push(furtherSplit[2]); total.push(furtherSplit[4]); // alert(furtherSplit[0]); //This is showing the name correctly } </script>

Don't use var name since it's reserved for the window scope and Quirks to "" String.不要使用var name ,因为它是为window scope 和 Quirks to "" String 保留的。
In contrast, by using const , let , (instead of var ) your variables are now instantiated and inside its own block scope (not window ).相反,通过使用constlet ,(而不是var )你的变量现在被实例化并在它自己的块 scope (不是window )内。
Also, when you do a for loop with < use just length , not length - 1此外,当您使用<执行 for 循环时,仅使用length ,而不是length - 1

 const str = `PP 0800 / 00 / XX: Units: 2: Total: 4.70 || PP 0800 / 00 / XX: Units: 1: Total: 2.35 ||`; const lines = str.split(/\n/); const name = []; const unit = []; const total = []; for (let i = 0; i < lines.length; i++) { const parts = lines[i].replace("||", "").split(":"); name.push(parts[0].trim()); unit.push(parts[2].trim()); total.push(parts[4].trim()); } console.log(name, unit, total)

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

相关问题 我一直收到这个错误“Uncaught TypeError:name.push不是一个函数 <anonymous> :10:7” - i keep getting this error “Uncaught TypeError: name.push is not a function at <anonymous>:10:7” 使用 FOR 循环将字符串推入空数组 - Pushing string into an empty array with FOR Loop 角度推入数组给出:TypeError:无法读取未定义的属性“ push” - Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined array.prototype.reduce:在箭头函数中将字符串值推送到空数组 - array.prototype.reduce: Pushing string value to empty array within arrow function 类型错误:list.push 不是推送到 localStorage 时的函数 - TypeError: list.push is not a function when pushing to localStorage 将函数的结果推入空数组 - push the result of a function into an empty array Javascript将未知名称函数推入数组 - Javascript pushing unknown name function into array Array.push 为 array.push 抛出 TypeError 不是函数 - Array.push throwing TypeError for array.push is not a function 如何为.push函数制作数组。 收到“ typeError:.push不是函数”? - How to make array for a .push function. Getting “typeError: .push is not a function”? Lambda Function:尽管将 object 推入 ZC1C425268E68385D1AB5074C17A9F4 中,但数组仍返回空 - Lambda Function: array returning empty despite pushing object to it inside function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM