2

我有一个 Unicode 值列表,[65, 66, 67]我想要相应的字符串"ABC"。我查看了文档并找到了String.fromCharCode()我需要做的功能。唯一的问题是参数需要是一个数字序列

所以如果我使用String.fromCharCode([65, 66, 67])它会给我" ".

有没有一种方法可以将列表视为函数的序列

4

4 回答 4

5

您需要使用... spread Syntax传播数组。

console.log(String.fromCharCode(...[65, 66, 67]));

来自MDN

扩展语法允许在预期零个或多个参数(对于函数调用)或元素(对于数组字面量)的地方扩展数组表达式等可迭代对象,或者在需要零个或多个键的地方扩展对象表达式。值对(用于对象文字)是预期的。

于 2017-07-06T20:24:24.957 回答
2

在列表上映射然后加入:

var s = [65, 66, 67].map(x => String.fromCharCode(x)).join("");
console.log(s);

于 2017-07-06T20:24:14.700 回答
1

/* You can map over the list and the value you need will be output to anoter list */
var charCodes = [65, 66, 67],
    stringsFromCharCodes = charCodes.map(item => String.fromCharCode(item));

console.log('new list: ', stringsFromCharCodes);

于 2017-07-06T20:27:03.167 回答
1

你可以使用 apply 来解决这个问题

var chars = [65,66,67]
var s = String.fromCharCode.apply({}, chars)
console.log(s);

于 2017-07-06T20:27:47.987 回答