【问题标题】:How to remove space in keys of a JSON object如何删除 JSON 对象的键中的空格
【发布时间】:2016-11-25 11:52:16
【问题描述】:

我有如下输出:

output = {
  "New Classroom": [{
    "Name": "Apple",
    "Age": "6",
    "Percentage": "24.00%"
  }, {
    "Name": "Orange",
    "Age": "5",
    "Percentage": "9.88%"
  }, {
    "Name": "Green",
    "Age": "2",
    "Percentage": "27.27%"
  }, {
    "Name": "Grey",
    "Age": "6",
    "Percentage": "12.63%"
  }]
}

如何将New Classroom 替换为NewClassroom 并且新教室并不总是“新教室”。它可能是不同的文本

ob = JSON.parse(output);

alert(Object.keys(ob))

当我这样做时,我将Newclassroom 作为密钥

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您可以遍历收到的对象中的顶级属性名称,检测带有空格的任何内容,然后删除空格。 (您不需要,它们是完全有效的属性名称,但如果您愿意,可以。)

    var output = { "New Classroom": [{"Name": "Apple","Age": "6","Percentage": "24.00%"},{"Name": "Orange","Age": "5","Percentage": "9.88%"},{"Name": "Green","Age": "2","Percentage": "27.27%"},{"Name": "Grey","Age": "6","Percentage": "12.63%"}]};
    var name, newName;
    // Loop through the property names
    for (var name in output) {
      // Get the name without spaces
      newName = name.replace(/ /g, "");
      // If that's different...
      if (newName != name) {
        // Create the new property
        output[newName] = output[name];
        // Delete the old one
        delete output[name];
      }
    }
    console.log(output);

    请注意,在对象上使用delete 会降低后续属性查找的性能。 99.99% 的时间,这没关系。如果这对您来说很重要,请创建一个 new 对象而不是就地修改它:

    var output = { "New Classroom": [{"Name": "Apple","Age": "6","Percentage": "24.00%"},{"Name": "Orange","Age": "5","Percentage": "9.88%"},{"Name": "Green","Age": "2","Percentage": "27.27%"},{"Name": "Grey","Age": "6","Percentage": "12.63%"}]};
    var name, newName;
    var newOutput = {};
    // Loop through the property names
    for (var name in output) {
      // Get the name without spaces
      newName = name.replace(/ /g, "");
      
      // Copy the property over
      newOutput[newName] = output[name];
    }
    console.log(newOutput);

    【讨论】:

    • 所以如果Percentage 有一些空格,这将不起作用?
    • @Mahi:问题不是我的印象。不,它不会,因为我们只处理顶级对象。当然,相同的主体可以应用于从属数组中的对象...
    【解决方案2】:
    • 使用Object.keys获取对象的所有键
    • 使用String#replace 替换String 中的字符

    var obj = {
      "New Classroom": [{
        "Name": "Apple",
        "Age": "6",
        "Percentage": "24.00%"
      }, {
        "Name": "Orange",
        "Age": "5",
        "Percentage": "9.88%"
      }, {
        "Name": "Green",
        "Age": "2",
        "Percentage": "27.27%"
      }, {
        "Name": "Grey",
        "Age": "6",
        "Percentage": "12.63%"
      }]
    };
    
    Object.keys(obj).forEach(function(key) {
      var replaced = key.replace(' ', '');
      if (key !== replaced) {
        obj[replaced] = obj[key];
        delete obj[key];
      }
    });
    console.log(obj);

    注意:只考虑一次出现的空格,如果space出现多次,则可以使用RegEx

    【讨论】:

    • 使用正则表达式替换,否则只替换第一个空格。
    【解决方案3】:

    循环进入json的每个键,然后解析。

    尝试正则表达式

    var word = "New Classroom"
    word = word.replace(/\s/g, '');
    console.log(word)

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2014-05-26
      • 2017-10-27
      • 1970-01-01
      • 2015-07-13
      • 2015-12-24
      相关资源
      最近更新 更多