拆分字符串值nodejs
splitting string values nodejs
我正在尝试从数组中拆分我的值。
看起来像这样:
[ 'xs', 'small', 'medium' ]
我想将它们分开,以便它们在我的数据库中显示如下:
xs small medium
到目前为止我有这个:
const str = size;
const words = str.split(',');
if (words.length > 0) {
for (i = 0; i < words.length; i++) {
console.log("Words: " + words[i])
}
}
但我收到错误
TypeError: str.split is not a function
不知道为什么,认为这行得通吗?谢谢
.split()
是 Strings
的函数,而不是 Arrays
。我猜你想加入他们,对吧?
const str = size;
let data = str.join(" ");
console.log(data);
我正在尝试从数组中拆分我的值。
看起来像这样:
[ 'xs', 'small', 'medium' ]
我想将它们分开,以便它们在我的数据库中显示如下:
xs small medium
到目前为止我有这个:
const str = size;
const words = str.split(',');
if (words.length > 0) {
for (i = 0; i < words.length; i++) {
console.log("Words: " + words[i])
}
}
但我收到错误
TypeError: str.split is not a function
不知道为什么,认为这行得通吗?谢谢
.split()
是 Strings
的函数,而不是 Arrays
。我猜你想加入他们,对吧?
const str = size;
let data = str.join(" ");
console.log(data);