【发布时间】:2019-10-01 09:15:36
【问题描述】:
我有一个可以包含 Symbol() 项的数组。 Array.toSring 带来了异常。
const s = [10, 'abc', Symbol('test')].toString(); // this throws an exception
console.log([10, 'abc', Symbol('test')]); // this works
将此类数组转换为字符串的最佳方法是什么(就像 console.log 一样)?
【问题讨论】:
-
您会收到错误,因为您没有将 Symbol() 转换为 string ,而是将字符串转换为对象。它应该是 const s = [10, 'abc', Symbol('test').toString()];
标签: javascript arrays