如何将给定树 json 重构为 javascript 中的输出
How to restructure the given tree json to the output in javascript
我有一个 JSON 文件,该文件采用特定结构(请参阅树),但我也需要它的结构与预期输出类似。
是否可以在JS中重组数据?如果是这样,您将如何处理?
我需要帮助将树重组或映射到预期输出。希望大家能帮我解决这个重构问题
const tree = [
{
"type": "object",
"name": "pets",
"child":
[
{
type: "array",
name: "properties",
"child":
[
{
type: "object",
name: "PK",
},
{
type: "object",
name: "PO",
},
{
type: "object",
name: "PS",
},
{
type: "object",
name: "PA",
child: [{type: "array", name: "list"}, ]
},
]
},
{
type: "object",
name: "get",
}
]
},
]
const expectedResult = [
{
pets:
{
properties:
[
{
name: "PK"
},
{
name: "PO"
},
{
name: "PS"
},
{
"PA" :
{
list: []
}
}
],
get: {}
}
},
]
- 解析您的 json 数据
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
- 映射数据以满足您的需求
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/map
(或遍历它,任何适合你的)
您可以为各种类型及其函数获取一个对象来构建映射子项所需的结构。
const
tree = [{ type: "object", name: "pets", child: [{ type: "array", name: "properties", child: [{ type: "object", name: "PK" }, { type: "object", name: "PO" }, { type: "object", name: "PS" }, { type: "object", name: "PA", child: [{ type: "array", name: "list" }] }] }, { type: "object", name: "get" }] }],
types = {
object: (name, child) => child
? { [name]: Object.assign({}, ...child.map(fn)) }
: { name: name },
array: (name, child = []) => ({ [name]: child.map(fn) })
},
fn = ({ type, name, child }) => types[type](name, child),
result = tree.map(fn);
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
首先,我是小姐,不是先生
这是一个详细的示例,这不是最佳做法,但我认为这是更容易理解的方法。
// First create an empty array to store your results.
let expectedResults = [];
// Loop through you tree object
for (let i in tree) {
// create empty object for each tree object
let treeObj = {};
// get the name of the object
const objName = tree[i]['name'];
// assign it as a key and set it's value as object
treeObj[objName] = {};
// get the children
const objChild = tree[i]['child'];
// loop through the children
for (let j in objChild) {
// get the name
const childName = objChild[j].name;
// check the type and assign either as object either as array
if (objChild[j].type === 'object') {
treeObj[objName][childName] = {};
}
if (objChild[j].type === 'array') {
treeObj[objName][childName] = [];
const childArr = objChild[j].child;
for (let k in childArr) {
if (childArr[k].type === 'object') {
treeObj[objName][childName].push({
name: childArr[k].name
});
}
if (childArr[k].type === 'array') {
// and so on
}
}
}
}
expectedResults.push(treeObj);
}
我有一个 JSON 文件,该文件采用特定结构(请参阅树),但我也需要它的结构与预期输出类似。
是否可以在JS中重组数据?如果是这样,您将如何处理? 我需要帮助将树重组或映射到预期输出。希望大家能帮我解决这个重构问题
const tree = [
{
"type": "object",
"name": "pets",
"child":
[
{
type: "array",
name: "properties",
"child":
[
{
type: "object",
name: "PK",
},
{
type: "object",
name: "PO",
},
{
type: "object",
name: "PS",
},
{
type: "object",
name: "PA",
child: [{type: "array", name: "list"}, ]
},
]
},
{
type: "object",
name: "get",
}
]
},
]
const expectedResult = [
{
pets:
{
properties:
[
{
name: "PK"
},
{
name: "PO"
},
{
name: "PS"
},
{
"PA" :
{
list: []
}
}
],
get: {}
}
},
]
- 解析您的 json 数据
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
- 映射数据以满足您的需求
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/map
(或遍历它,任何适合你的)
您可以为各种类型及其函数获取一个对象来构建映射子项所需的结构。
const
tree = [{ type: "object", name: "pets", child: [{ type: "array", name: "properties", child: [{ type: "object", name: "PK" }, { type: "object", name: "PO" }, { type: "object", name: "PS" }, { type: "object", name: "PA", child: [{ type: "array", name: "list" }] }] }, { type: "object", name: "get" }] }],
types = {
object: (name, child) => child
? { [name]: Object.assign({}, ...child.map(fn)) }
: { name: name },
array: (name, child = []) => ({ [name]: child.map(fn) })
},
fn = ({ type, name, child }) => types[type](name, child),
result = tree.map(fn);
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
首先,我是小姐,不是先生
这是一个详细的示例,这不是最佳做法,但我认为这是更容易理解的方法。
// First create an empty array to store your results.
let expectedResults = [];
// Loop through you tree object
for (let i in tree) {
// create empty object for each tree object
let treeObj = {};
// get the name of the object
const objName = tree[i]['name'];
// assign it as a key and set it's value as object
treeObj[objName] = {};
// get the children
const objChild = tree[i]['child'];
// loop through the children
for (let j in objChild) {
// get the name
const childName = objChild[j].name;
// check the type and assign either as object either as array
if (objChild[j].type === 'object') {
treeObj[objName][childName] = {};
}
if (objChild[j].type === 'array') {
treeObj[objName][childName] = [];
const childArr = objChild[j].child;
for (let k in childArr) {
if (childArr[k].type === 'object') {
treeObj[objName][childName].push({
name: childArr[k].name
});
}
if (childArr[k].type === 'array') {
// and so on
}
}
}
}
expectedResults.push(treeObj);
}