[英]replace/replaceAll is not a function
I'm trying to use replace in order to remove some characters in a 2d array of strings, but I keep incountering an issue:我正在尝试使用替换来删除二维字符串数组中的一些字符,但我一直遇到一个问题:
dataArr[j,k] = dataArr[j,k].replaceAll(mCH, "");
^
TypeError: dataArr[(j , k)].replaceAll is not a function
at Object.<anonymous> (C:\Users\magshimim\say-hi-to-sheli-from-me\rafaels-crypto-ninja\thingy\server\index.js:45:35)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
this is the part of my code using the function:这是我使用该函数的代码的一部分:
for (let j = 0; j < dataArr.length; j++)
{
for (let k = 0; k < dataArr[j].length; k++)
{
dataArr[j][k] = dataArr[j][k].replaceAll('"', "");
dataArr[j][k] = dataArr[j][k].replaceAll(":", "");
dataArr[j][k] = dataArr[j][k].replaceAll("I", "");
dataArr[j][k] = dataArr[j][k].replaceAll("L", "");
dataArr[j][k] = dataArr[j][k].replaceAll("S", "");
dataArr[j][k] = dataArr[j][k].replaceAll("}", "");
}
}
Do I need to download anything?我需要下载什么吗? maybe there's a problem with my code?也许我的代码有问题?
That's not how you index a 2-dimensional array (well, array of arrays; there are no 2-dimensional arrays in JavaScript).这不是您索引二维数组的方式(好吧,数组数组;JavaScript 中没有二维数组)。
You're looking for dataArr[j][k]
, not dataArr[j,k]
.您正在寻找dataArr[j][k]
,而不是dataArr[j,k]
。
[j, k]
evaluates to just k
since the comma is parsed as the comma sequence operator ; [j, k]
计算结果为k
,因为逗号被解析为逗号序列运算符; j
is evaluated and thrown away, and k
is the result. j
被评估并丢弃, k
是结果。 It is then used to index the top-level dataArr
, and indeed, replaceAll
is not a method on the arrays nested within.然后使用它来索引顶级dataArr
,实际上, replaceAll
不是嵌套在其中的数组的方法。
You could also rewrite this as a double map
and use a regexp so you don't need to spell out all of those replacements:您还可以将其重写为双map
并使用正则表达式,因此您无需拼出所有这些替换:
dataArr = dataArr.map(row => row.map(c => String(c).replace(/[":ILS}]+/g, "")));
You can use the .replace
function instead of replaceAll
, if the later is not available.如果后者不可用,您可以使用.replace
函数代替replaceAll
。
"any string".replace(new RegExp("to replace", "g"), "new text")
This is by using new RegExp("text to replace", "g")
as a string to search这是通过使用new RegExp("text to replace", "g")
作为要搜索的字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.