简体   繁体   English

如何将char添加到javascript对象键和值?

[英]How to add char to javascript object keys and values?

I am trying to add '_x' to every object key and '_y' to every object value. 我试图将'_x'添加到每个对象键,并将'_y'到每个对象值。 This is the code: 这是代码:

var data = {
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
      "title": "S",
      "GlossList": {
        "GlossEntry": {
          "ID": "SGML",
          "SortAs": "SGML",
          "GlossTerm": "Standard Generalized Markup Language",
          "Acronym": "SGML",
          "Abbrev": "ISO 8879:1986",
          "GlossDef": {
            "para": "A meta-markup language, used to create markup languages such as DocBook.",
            "GlossSeeAlso": "GMLXML"
          },
          "GlossSee": "markup"
        }
      }
    }
  }
}

function treee(data) {
  Object.keys(data).map( function (key) {
    if(Object.keys(data[key]).length == 0){
      data[key] =  {[key + "_x"]: data[key] + "_y"};
    }
    else{
      data[key] =  { [key + "_x"]: treee(data[key]) };
    }
  });
}

It is not working and I don't know why. 它不起作用,我也不知道为什么。 Can you please tell what is wrong? 你能告诉我出什么问题了吗?

Basically you need the new key instead of the old one and delete the actual one. 基本上,您需要新密钥而不是旧密钥,然后删除实际的密钥。

Another part is to change an inner object, if necessaray. 如果需要,另一部分是更改内部对象。

 function treee(data) { Object.keys(data).map(function (key) { if (data[key] && typeof data[key] === 'object') { treee(data[key]); } data[key + '_x'] = typeof data[key] === 'string' ? data[key] + '_y' : data[key]; delete data[key]; }); } var data = { glossary: { title: "example glossary", GlossDiv: { title: "S", GlossList: { GlossEntry: { ID: "SGML", SortAs: "SGML", GlossTerm: "Standard Generalized Markup Language", Acronym: "SGML", Abbrev: "ISO 8879:1986", GlossDef: { para: "A meta-markup language, used to create markup languages such as DocBook.", GlossSeeAlso: "GMLXML" }, GlossSee: "markup" } } } } }; treee(data); console.log(data); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

This is easier to do if you convert the object to JSON, then use string replacements: 如果将对象转换为JSON,然后使用字符串替换,则这样做更容易:

 var data = { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": "GMLXML" }, "GlossSee": "markup" } } } } } var jsonString = JSON.stringify(data), replacedValues = jsonString .replace(/([\\w.])"/g, '$1_y"') // Add _y to all strings in there. .replace(/_y":/g, '_x":') // Replace _y with _x for all keys. newData = JSON.parse(replacedValues); console.log(newData); 

Another option would be to use a regex to replace the keys, then use a reviver function to modify the values, when parsing the JSON: 另一种选择是在解析JSON时,使用正则表达式替换键,然后使用reviver函数修改值:

 var data = { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": "GMLXML" }, "GlossSee": "markup" } } } } } var jsonString = JSON.stringify(data) .replace(/":/g, '_x":'); // Add _x to all keys. var newData = JSON.parse(jsonString, function(key, val){ if(typeof val === 'string') // For every value, if the value is a string return val + '_y'; // Add `_y` return val; // Otherwise, just return the value. }); console.log(newData); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM