简体   繁体   English

.join ( ) 仅适用于 console.log ( )

[英].join ( ) Only Works in console.log ( )

When I use the console.log( ) function to join this array it returns the combined array, but when I use the .join( ) function outside of console.log( ), it does not combine the array.当我使用 console.log( ) 函数加入这个数组时,它返回组合的数组,但是当我在 console.log( ) 之外使用 .join( ) 函数时,它不会组合数组。 How do I combine this array to create a single string with no comma outside of console.log( )?如何组合此数组以在 console.log( ) 之外创建一个没有逗号的单个字符串?

var string = stringArray.map(string => "&sources=" + string);
console.log(stringURL.join(''));

stringURL.join('');
console.log(stringURL);

.join() is a function that returns the joined string. .join()是一个返回连接字符串的函数。 So when you call it, it calculates the value and then returns it.所以当你调用它时,它会计算值然后返回它。

Store it in a variable.将其存储在变量中。

var joinedString = stringURL.join('');

Your problem is that stringURL.join('');你的问题是stringURL.join(''); does modify stringURL but give a new string.确实修改了 stringURL 但给出了一个新字符串。 so the best is to make所以最好是让

var new_string = stringURL.join('');
console.log(new_string);

You can't have a variable name called string, it's protected.你不能有一个名为 string 的变量名,它是受保护的。 Change it to something else and it will work.将其更改为其他内容,它将起作用。

Here's a complete list of reserved keywords and here is an explanation from Mozilla MDN这是保留关键字的完整列表, 这是 Mozilla MDN 的解释

var mappedString = stringArray.map(string => "&sources=" + string);
console.log(mappedString.join(''));

var joinedString = mappedString.join('')

console.log(joinedString);

That should do!应该这样做!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM