从列表中获取包含 JavaScript 中的 Unicode 的字符串

Get string from a list contain Unicode in JavaScript

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

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

有没有办法允许列表被视为函数的序列[=30] =]?

地图上榜然后加入:

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

您需要使用 ... spread Syntax.

展开数组

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

来自MDN

Spread syntax allows an iterable such as an array expression to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

/* 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);

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

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