[英]how to encode/ change the format of existing json to another using angular/javascript
here im having a json in below format 这里我有以下格式的json
Present Format 目前格式
{
"info": {
"laptop": {
},
"config": {
"properties": {
"ram": {
},
"processor": {
},
"hdd": {
}
}
},
"link": {
},
"name": {
},
"company": {
"properties": {
"model": {
},
"maker": {
"type": "integer"
},
"country": {
"type": "text"
},
"enterprise": {
}
}
}
}
}
i am using some plugin like ngx-tree, primeng plugins for which data needs to be in different format like below 我正在使用某些插件,例如ngx-tree,primeng插件,其数据需要采用以下不同格式
Required Format 必填格式
[
{
name: 'laptop',
},
{
name: 'config',
children: [
{ name: 'ram', children: [] },
{ name: 'processor' },
{name:'hdd'}
]
},
{
name: 'link',
children: []
},
{
name: 'name',
children: []
},
{
name: 'company',
children: [
{ name: 'model' },
{ name: 'maker' },
{ name: 'country' },
{ name: 'enterprise' }
]
}
];
now my issue is how can i change my data from the present format to required format ? 现在我的问题是如何将数据从当前格式更改为所需格式? is there any suggestion so that i can make that change 有什么建议可以让我改变吗
You can recursively walk over the input object to map it to the required format with something like this: 您可以递归地遍历输入对象,将其映射为所需的格式,如下所示:
function recursiveConvert(input) {
let converted = []
for(i in input){
converted.push({
name: i,
children: []
})
if(input[i].properties) {
converted[converted.length - 1].children.push(recursiveConvert(input[i].properties))
}
}
return converted
}
If you want no empty children arrays simply change it to: 如果您不希望有空的子数组,只需将其更改为:
function recursiveConvert(input) {
let converted = []
for(i in input){
converted.push({
name: i
})
if(input[i].properties) {
let lastInsert = converted[converted.length - 1]
lastInsert.children = [recursiveConvert(input[i].properties)]
}
}
return converted
}
be sure to call it with recursiveConvert(myInput.info)
since it doesn't expect that wrapper object. 请确保使用recursiveConvert(myInput.info)
调用它,因为它不需要该包装对象。
You can do: 你可以做:
const json = {"info": {"laptop": {},"config": {"properties": {"ram": {},"processor": {},"hdd": {}}},"link": {},"name": {},"company": {"properties": {"model": {},"maker": {"type": "integer"},"country": {"type": "text"},"enterprise": {}}}}}; const result = Object.keys(json.info).map(k => ({ name: k, children: json.info[k].properties ? Object.keys(json.info[k].properties).map(kk => ({name: kk})) : [] })); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Here's a one line, purely functional solution: 这是一个单行的纯功能解决方案:
const input = { "info": { "laptop": { }, "config": { "properties": { "ram": { }, "processor": { }, "hdd": { } } }, "link": { }, "name": { }, "company": { "properties": { "model": { "properties": { "apple": {}, "banana": {}, "pear": {} } }, "maker": { "type": "integer" }, "country": { "type": "text" }, "enterprise": { } } } } } const convert = input => Object .entries(input) .map(([name, value]) => ({ name, children: convert(value.properties || {}) })) console.log(convert(input.info));
Here's an es2015 version: 这是es2015版本:
function convert(input) {
return Object.keys(input).map(function(name) {
return {
name: name,
children: convert(input[name].properties || {})
};
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.