javascript .split() 不是函数错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51831389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:43:03  来源:igfitidea点击:

.split() is not a function error

javascriptnode.js

提问by user1584421

I have an array that i transformed it into text with array-to-txt module.

我有一个数组,我使用 array-to-txt 模块将其转换为文本。

Instead i wanted it transformed to a big string, but i wanted a newline after every index of the array (the afforementioned module does this automatically).

相反,我希望将其转换为一个大字符串,但我希望在数组的每个索引之后都有一个换行符(上述模块会自动执行此操作)。

So i wrote this:

所以我写了这个:

result.toString();
result = result.split(",").join("\n");

Where result is the array. It didn't work, so then i tried this:

结果是数组。它没有用,所以我尝试了这个:

result.toString();
var output = result.split(",").join("\n");

Still i get the TypeError: result.split is not a functionerror.

我仍然收到TypeError: result.split is not a function错误。

回答by charlietfl

The problem you have is result.toString()doesn't modify the original array...it only returnsa string.

您遇到的问题是result.toString()不修改原始数组...它只返回一个字符串。

You would need something like:

你需要这样的东西:

var str = result.toString()
var output= str.split(',').join('\n');

However there is no need to convert to string and immediately back to array when all you really need is:

但是,当您真正需要的是:

var output = result.join('\n')

回答by kebek

to convert the array into string use var k = String(result); var outPut = k.split(',').join('\n');

将数组转换为字符串使用 var k = String(result); var outPut = k.split(',').join('\n');

回答by Dzenis H.

const result = ['a','b','c'];

const data = result.join('\n');

console.log(data);