javascript - 递归 Javascript camelCase 解释

标签 javascript json recursion

我正在尝试了解此递归函数如何将 JSON 键转换为驼峰命名法。有人可以对每一行发表评论和/或添加解释吗?谢谢!

function toCamel(o) {
  var newO, origKey, newKey, value;

  if (o instanceof Array) {
    return o.map(function(value) {
      if (typeof value === 'object') {
        value = toCamel(value);
      }

      return value;
    });
  } else {
    newO = {};

    for (origKey in o) {
      if (o.hasOwnProperty(origKey)) {
        newKey = (origKey.charAt(0).toLowerCase() + origKey.slice(1) || origKey).toString();
        value = o[origKey];

        if (value instanceof Array || (value !== null && value.constructor === Object)) {
          value = toCamel(value);
        }

        newO[newKey] = value;
      }
    }
  }

  return newO;
}

最佳答案

我已经评论了代码以解释发生了什么,如果它没有意义,请告诉我!它基本上是从您传递给它的内容 (json) 中遍历每个项目,如果它是数组或对象,则再次处理每个项目。它将 key 转换为字符串并将第一个字符设置为小写。

// create a test json array;
var json = {
"TestArr": [
    {"TestKey": 1, "ItemIDs": [0, 1, 2, 3, 4]},
    {"TestKey": 2, "ItemIDs": [5, 6, 7, 8, 9]}
  ]
};

function toCamel(o) {
  var newO, origKey, newKey, value
  // if the passed parameter is an Array...
  if (o instanceof Array) {
    // iterate through each item in the array... 
    return o.map(function(value) {
        // if the current item is an object...
      if (typeof value === "object") {
        // send the current item to this function...
        // this time as an oblect
        value = toCamel(value)
      }
      // return the current item
      return value
    });
  // if the passed item is an object...
  } else {
    // create a temporary object
    newO = {}
    // for each key in the object
    for (origKey in o) {
        // check to make sure the object has a property of the current key
      if (o.hasOwnProperty(origKey)) {
        // convert the key to a string, then make the first character in the string lowercase
        newKey = (origKey.charAt(0).toLowerCase() + origKey.slice(1) || origKey).toString()
        // set the value var equal to the current key value
        value = o[origKey]
        // if value is an array OR the value isn't null AND its constructor is an Object...
        // (ensure the value is an array or object)
        if (value instanceof Array || (value !== null && value.constructor === Object)) {
            // set value to this function for more processing
          value = toCamel(value)
        }
        // set the temporary object key value to the new processed value or the original value if not an array/object
        newO[newKey] = value
      }
    }
  }
  // return the finished/formatted JSON
  return newO;
}

// set a variable to the value returned from the toCamel function
var test = toCamel(json);
// write out the result in the console
console.log(test);

关于javascript - 递归 Javascript camelCase 解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290969/

相关文章:

Javascript从表单单选选项列表或文本字段(无论使用哪个)中获取选定的值,并输出到文本?

javascript - React Native & TextInput : How to get what the use type one by one for Android?

mysql - 从MySQL数据库查询返回node.js中的[Object] [Object],并且JSON.stringify()似乎不起作用

python - 子类化 python 字典

c - 这个递归程序如何反转所有字符

javascript - React typescript Object.assign 返回类型

javascript - 当复选框长度大于 1 时,提交按钮不启用

javascript - 当我 “Pull to refresh” 时,我如何清理缓存?使用 Ionic 框架和 AngularJS

recursion - 错误: Statement Function is recursive

c# - 在具有子列表<T> 的列表<T> 中递归读取 XML 树结构