【问题标题】:Can I pass an array into fromCharCode我可以将数组传递给 fromCharCode
【发布时间】:2012-03-30 03:06:51
【问题描述】:

我可能在这里问错了问题,所以如果答案在另一个线程上,我深表歉意……但我环顾四周无济于事。

在一个 sn-p 中,为什么这不起作用?

array = [72,69,76,76,79];
document.write(String.fromCharCode(array));

我正在一个数组中收集关键事件,并希望能够在出现提示时将它们写为字符。虽然这有效:

document.write(String.fromCharCode(72,69,76,76,79));

当我将它作为数组传递时,我似乎无法让它工作。我还尝试先将数组转换为 toString() 以及 array.join(",");创建一个逗号分隔的列表......但什么都没有。有任何想法吗?有没有更好的方法将我在数组中收集的值转换为字符?

【问题讨论】:

    标签: javascript arrays fromcharcode


    【解决方案1】:

    你可以使用函数的apply()方法...

    document.write(String.fromCharCode.apply(null, array));
    

    jsFiddle.

    ES6 可以使用扩展运算符...

    document.write(String.fromCharCode(...array));
    

    您也可以使用数组的reduce() 方法,但较旧的IE 不支持它。您可以 shim 它,但更好地支持 apply() 方法。

    document.write(array.reduce(function(str, charIndex) {
        return str += String.fromCharCode(charIndex);
    }, ''));​
    

    jsFiddle.

    【讨论】:

    • 考虑到这个技巧有潜在的危险。如果您尝试以这种方式构造非常长的字符串(例如,为 base64 编码准备 ArrayBuffer),您将遇到范围异常。
    【解决方案2】:

    是的,您可以使用apply() 调用一个函数,该函数将数组作为其参数传入:

    array = [72,69,76,76,79];
    document.write(String.fromCharCode.apply(String, array));
    

    【讨论】:

    • @alex 是的,我认为这并不重要,我选择了String,因为这是所有酷孩子都在做的事情。
    • 我更喜欢:String.fromCharCode.apply(window.navigator.geolocation.getCurrentPosition, array)
    • 太棒了,这也是 wrx :) thnx!
    • @PaulP.R.O.当你忘记它时,我写了那条评论。现在一切都好:)
    • @alex 哦,我明白了,哈哈。该死,我以为我的速度太快了,任何人都看不到我的错误哈​​哈
    【解决方案3】:

    如果您使用.apply() 调用fromCharCode() 函数,您可以向其传递一个数组,该数组将被转换为函数的参数,如下所示:

    document.write(String.fromCharCode.apply(this, array));
    

    你可以在这里看到它的工作原理:http://jsfiddle.net/jfriend00/pfLLZ/

    【讨论】:

    • @Nick 是的,它们不是一辆坏车。
    【解决方案4】:

    该方法更适合这样使用:

    document.write(String.fromCharCode(72,69,76,76,79));
    

    当方法需要多个参数作为列表时,您将传入一个数组。

    【讨论】:

    • 这对于大字符串可能会变得混乱
    • 它们在 chrome 中的硬性限制为 65536 个条目。
    【解决方案5】:

    您可能必须使用如下循环

    for(var i = 0; i < array.length; i++)
           document.write(String.fromCharCode(array[i]));
    }
    

    【讨论】:

      【解决方案6】:

      在新版本的javascript中,你可以使用spread syntax

      const array = [72,69,76,76,79];
      document.write(String.fromCharCode(...array));
      

      【讨论】:

        【解决方案7】:

        这里有两种方法:

        var arr=[119,119,119,46,87,72,65,75,46,99,111,109];
        document.write(eval('String.fromCharCode('+arr+')'));
        
        document.write('<hr>');
        
        var arr='119,119,119,46,87,72,65,75,46,99,111,109';
        document.write(eval('String.fromCharCode('+arr+')'));

        【讨论】:

        • 很遗憾,依赖于数组到字符串的隐式转换不是立即可见的。
        【解决方案8】:

        这是一个函数:

        var unCharCode = function(x) {
          return this["eval"]("String['fromCharCode'](" + x + ")");
        };
        
        document.write(unCharCode([ 119, 119, 119, 46, 87, 72, 65, 75, 46, 99, 111, 109 ]));
        
        document.write("<hr>");
        
        document.write(unCharCode("119,119,119,46,87,72,65,75,46,99,111,109"));

        【讨论】:

        • 为什么eval 使用括号表示法?为什么要eval
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 2016-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        相关资源
        最近更新 更多