19

I have an object like

{ "status": "success", "auth": { "code": "23123213", "name": "qwerty asdfgh" } }

I want to convert it to dot notation (one level) version like:

{ "status": "success", "auth.code": "23123213", "auth.name": "qwerty asdfgh" }

Currently I am converting the object by hand using fields but I think there should be a better and more generic way to do this. Is there any?

Note: There are some examples showing the opposite way, but i couldn't find the exact method.

Note 2: I want it for to use with my serverside controller action binding.

4

7 回答 7

37

您可以递归地将属性添加到新对象,然后转换为 JSON:

var res = {};
(function recurse(obj, current) {
  for(var key in obj) {
    var value = obj[key];
    var newKey = (current ? current + "." + key : key);  // joined key with dot
    if(value && typeof value === "object") {
      recurse(value, newKey);  // it's a nested object, so do it again
    } else {
      res[newKey] = value;  // it's not an object, so set the property
    }
  }
})(obj);
var result = JSON.stringify(res);  // convert result to JSON
于 2012-11-04T13:16:06.137 回答
10

undefined这是您获得第一个前缀时的修复/破解。(我做过)

var dotize = dotize || {};

dotize.parse = function(jsonobj, prefix) {
  var newobj = {};
  function recurse(o, p) {
    for (var f in o)
    {
      var pre = (p === undefined ? '' : p + ".");
      if (o[f] && typeof o[f] === "object"){
        newobj = recurse(o[f], pre + f);
      } else {
        newobj[pre + f] = o[f];
      }
    }
    return newobj;
  }
  return recurse(jsonobj, prefix);
};
于 2013-09-02T21:21:39.200 回答
5

您可以使用 NPM点对象( Github ) 将对象转换为点符号,反之亦然。

于 2019-03-18T18:23:00.360 回答
1

我写了另一个带有前缀功能的函数。我无法运行您的代码,但我得到了答案。谢谢

https://github.com/vardars/dotize

var dotize = dotize || {};

dotize.convert = function(jsonobj, prefix) {
    var newobj = {};

    function recurse(o, p, isArrayItem) {
        for (var f in o) {
            if (o[f] && typeof o[f] === "object") {
                if (Array.isArray(o[f]))
                    newobj = recurse(o[f], (p ? p + "." : "") + f, true); // array
                else {
                    if (isArrayItem)
                        newobj = recurse(o[f], (p ? p : "") + "[" + f + "]"); // array item object
                    else
                        newobj = recurse(o[f], (p ? p + "." : "") + f); // object
                }
            } else {
                if (isArrayItem)
                    newobj[p + "[" + f + "]"] = o[f]; // array item primitive
                else
                    newobj[p + "." + f] = o[f]; // primitive
            }
        }
        return newobj;
    }

    return recurse(jsonobj, prefix);
};
于 2012-11-04T13:24:36.343 回答
1

const sourceObj = { "status": "success", "auth": { "code": "23123213", "name": "qwerty asdfgh" } }
;

const { auth, ...newObj } = sourceObj;

const resultObj = {
  ...newObj,
  ..._.mapKeys(auth, (val, key) => `auth.${key}`)
}


// function approach
const dotizeField = (obj, key) => {
  const { ...newObj } = sourceObj;

  delete newObj[key];

  return {
    ...newObj,
    ..._.mapKeys(obj[key], (val, subKey) => `${key}.${subKey}`)
  }
}

const resultObj2 = dotizeField(sourceObj, 'auth');

console.log(sourceObj, resultObj, resultObj2);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

于 2021-01-12T13:01:54.477 回答
1

按照@pimvdb 所做的(他提交的一个紧凑而有效的解决方案),我添加了一些修改,让我拥有一个可以轻松导出的功能:

    function changeObjectToDotNotationFormat(inputObject, current, prefinalObject) {
      const result = prefinalObject ? prefinalObject : {}; // This allows us to use the most recent result object in the recursive call
      for (let key in inputObject) {
        let value = inputObject[key];
        let newKey = current ? `${current}.${key}` : key;
        if (value && typeof value === "object") {
          changeObjectToDotNotationFormat(value, newKey, result);
        } else {
          result[newKey] = value;
        }
      }
      return result;
    }
于 2021-03-29T11:11:52.540 回答
0

我认为这会更优雅......

const toDotNot = (input, parentKey) => Object.keys(input || {}).reduce((acc, key) => {
  const value = input[key];
  const outputKey = parentKey ? `${parentKey}.${key}` : `${key}`;

  // NOTE: remove `&& (!Array.isArray(value) || value.length)` to exclude empty arrays from the output
  if (value && typeof value === 'object' && (!Array.isArray(value) || value.length)) return ({ ...acc, ...toDotNot(value, outputKey) });

  return ({ ...acc, [outputKey]: value });
}, {});

const input = {a: {b: 'c', e: {f: ['g', null, {g: 'h'}]}}, d: []};
const output = toDotNot(input);

console.log(output);

结果是:

// output:
{
    "a.b": "c",
    "a.e.f.0": "g",
    "a.e.f.1": null,
    "a.e.f.2.g": "h",
    "d": []
}
于 2021-12-11T12:36:55.363 回答