【问题标题】:How to rename json key's value in IE如何在 IE 中重命名 json 键的值
【发布时间】:2018-08-16 17:05:04
【问题描述】:
function replaceJsonStringify( key, value1 ) {

    var $edit = $("#trafficLog");
    var currentValue = $edit.val();
    var jsonConnect = { "time":getLongTime(), [key] : value };
    var jsonObj = JSON.stringify(jsonConnect);
    var newValue = jsonObj + "<br>" + currentValue;

    $edit.val(replaceBR(newValue));   }

replaceJsonStringify( "connectFlag", connect );

我想重命名 json 键的值。

上述代码在 Chrome 中运行良好,但在 IE11、10 中无法运行..

我想知道如何重命名 json 键的值

代码中的问题是“[key] : value”

IE调试:SCRIPT1028: Expected identifier, string or number.

请回答这个问题。

【问题讨论】:

标签: javascript jquery json internet-explorer key


【解决方案1】:

我猜 IE 无法理解对象中尚未计算的属性。改变这个:

var jsonConnect = { "time":getLongTime(), [key] : value };

到这里:

var jsonConnect = { time: getLongTime() }
jsonConnect[key] = value;

【讨论】:

  • 在 Chrome 中,结果为 {"time":"2018-3-8 20:15:37","connectFlag":"connect"} 但在 IE 中,仍然出现错误。 ——
  • 我很抱歉!!那工作得很好!非常感谢
  • 没问题,欢迎采纳。干杯!
【解决方案2】:

IE 不支持计算属性名称。您需要替换此行:

var jsonConnect = { "time":getLongTime(), [key] : value };

用这个:

var jsonConnect = { "time":getLongTime() };
jsonConnect[key] = value;

更多详情请见this document

【讨论】:

  • 在 Chrome 中,结果是 {"time":"2018-3-8 20:15:37","connectFlag":"connect"} 但在 IE 中,仍然出现错误。
  • 我很抱歉!!那工作得很好!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 2019-04-11
  • 2020-02-02
  • 2018-09-21
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多