What characters are grouped with Array.from?

后端 未结 3 524
Happy的楠姐
Happy的楠姐 2021-02-03 19:06

I\'ve been playing around with JS and can\'t figure out how JS decides which elements to add to the created array when using Array.from(). For example, the followin

3条回答
  •  野性不改
    2021-02-03 19:26

    It's all about the code behind the characters. Some are coded in two bytes (UTF-16) and are interpreted by Array.from as two characters. Gotta check the list of the characters :

    http://www.fileformat.info/info/charset/UTF-8/list.htm

    http://www.fileformat.info/info/charset/UTF-16/list.htm

    function displayHexUnicode(s) {
      console.log(s.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(4,"0"),""));
    }
    
    displayHexUnicode('षि');
    
    console.log(Array.from('षि').forEach(x => displayHexUnicode(x)));


    function displayHexUnicode(s) {
      console.log(s.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(4,"0"),""));
    }
    
    displayHexUnicode('

提交回复
热议问题