简体   繁体   English

最优雅的方式来创建一个JSON对象

[英]Most elegant way to create a JSON Object

I hava a javacript object that look like this. 我有一个看起来像这样的javacript对象。

val = 
{
default: "version1"
version1: "abc.com"
version2: "def.com"
...
versionn:"xyz.com"
}

I want to convert this to a nested json like this 我想将它转换为这样的嵌套json

"newval": {
"website": {
    "url": "abc.com"
},
"versions": {
    "version1": {
        "website": {
            "url": "abc.com"
        }
    },
    "version2": {
        "website": {
            "url": "def.com"
        }
    }
}

} }

My version uses a lot if if statements and unnecessary variables. 如果if语句和不必要的变量,我的版本会使用很多。 Is there an elegant way to convert/create this JSON. 有没有一种优雅的方式来转换/创建这个JSON。

ie the default version is the one that comes under newval.website and all the version data is the value fir newval.website.versions 即默认版本是newval.website下的版本,所有版本数据都是fir newval.website.versions

Setting the .website property is pretty simple. 设置.website属性非常简单。 Just a matter of looking up the key that default holds. 只需查找default保存的密钥即可。

The other part can be done by filtering out the default key, and then mapping the remaining ones to a new list of objects that are assigned to the result using Object.assign . 另一部分可以通过过滤掉default密钥,然后将其余部分映射到使用Object.assign分配给结果的新对象列表来完成。

 var val = { default: "version1", version1: "abc.com", version2: "def.com", version3:"xyz.com" }; var res = { website: {url: val[val.default]}, versions: Object.assign( ...Object.keys(val) .filter(k => k !== "default") .map(k => ({[k]: {website: {url: val[k]} } })) )}; console.log(res); 


Here's another way that avoids needing the .filter() . 这是避免需要.filter()的另一种方法。 It assumes you don't mind making default non-enumerable on the original object. 它假设您不介意在原始对象上default非可枚举。

 var val = { default: "version1", version1: "abc.com", version2: "def.com", version3:"xyz.com" }; var res = { website: {url: val[Object.defineProperty(val, "default", {enumerable:false}).default]}, versions: Object.assign( ...Object.keys(val).map(k => ({[k]: {website: {url: val[k]} } })) )}; console.log(res); 

The property is still configurable, so you could reset it if desired. 该属性仍然是可配置的,因此您可以根据需要重置它。


Or using .entries() instead of .keys() with parameter destructuring in the .map() callback. 或者在.map()回调中使用.entries()而不是.keys()和参数解构。

 var val = { default: "version1", version1: "abc.com", version2: "def.com", version3:"xyz.com" }; var res = { website: {url: val[Object.defineProperty(val, "default", {enumerable:false}).default]}, versions: Object.assign( ...Object.entries(val).map(([k, v]) => ({[k]: {website: {url: v} } })) )}; console.log(res); 

My version: 我的版本:

var val = {
  default: 'version1',
  version1: 'abc.com',
  version2: 'def.com',
  version3: 'xyz.com'
}

var res = {
  website: {url: val[val.default]},
  versions: Object.entries(val).reduce(function(a, [k, v]) {
    if (k != 'default') a[k] = {website: {url: v}}
    return a
  }, {})
}

console.log(res)

And here's my version, just reducing the entries directly into the websites property, excluding the default property 这是我的版本,只是将条目直接减少到websites属性,不包括default属性

 var val = { default: "version1", version1: "abc.com", version2: "def.com", versionn: "xyz.com" } var result = { newval : {website : val.default}, versions : Object.entries(val).reduce((a,b)=>((b[0] != 'default'?a[b[0]]={website:b[1]}:0),a),{}) }; console.log(result) 

You could assign the part for default directly and the rest by iterating the keys and assigning to the object. 您可以通过迭代键并分配给对象来直接为默认值分配零件。

 var values = { default: "version1", version1: "abc.com", version2: "def.com", versionn: "xyz.com" }, result = { newval: { website: { url: values[values.default] }, versions: Object.assign(...Object.keys(values).map(k => k === 'default' ? {} : { [k]: { website: { url: values[k] } } })) } }; console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

A bit more efficient and compatible without function call: 没有函数调用更高效和兼容:

 val = { default:"version1", version1:"abc.com", version2:"def.com", versionn:"xyz.com" } newval = { website: { url: val[val.default] }, versions: { } } for (k in val) if (k !== 'default') newval.versions[k] = { website: { url: val[k] } } console.log({newval}) 

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

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