String.fromCodePoint(...[127482, 127480])
gives me a flag of the US ().
How do I turn the flag back to [127482, 127480]
?
You're looking for codePointAt
, perhaps using spread (etc.) to convert back to array and then mapping each of them.
console.log(theString.codePointAt(0)); // 127482
console.log(theString.codePointAt(2)); // 127480
// Note −−−−−−−−−−−−−−−−−−−−−−−−−−^
// It's 2 because the first code point in the string occupies two code *units*
or
const array = [...theString].map(s => s.codePointAt(0));
console.log(array); // [127482, 127480]
or skipping an interim step as Sebastian Simon pointed out via Array.from
and its mapping callback:
const array = Array.from(theString, s => s.codePointAt(0));
console.log(array); // [127482, 127480]
Example:
const theString = String.fromCodePoint(...[127482, 127480]); console.log(theString.codePointAt(0)); // 127482 console.log(theString.codePointAt(2)); // 127480 const array = [...theString].map(s => s.codePointAt(0)); console.log(array); // [127482, 127480] const array2 = Array.from(theString, s => s.codePointAt(0)); console.log(array2); // [127482, 127480]
Spread and Array.from
both work by using the strings iterator , which works by code points, not code units like most string methods do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.