javascript - 我想改变我的 json 结构,我该怎么做?
问题描述
我想改变我的 json 结构,我该怎么做?
我得到一个看起来像这样的json:
data = [
{
"fyyear": "2019-2020",
"kraid": 5,
"kraname": "CSAT",
"kracriteria": 18,
"lowerlimit": 2,
"upperlimit": 3.99,
"startrange": 50,
"endrange": 60
},
{
"fyyear": "2019-2020",
"kraid": 5,
"kraname": "CSAT",
"kracriteria": 18,
"lowerlimit": 2,
"upperlimit": 3.99,
"startrange": 50,
"endrange": 60
}
]
但我需要它在这个结构中:
data = [{
"fyyear": "2020-2021",
"kraid": null,
"kraname": "",
"kracriteria": [
{
"lowerlimit": "QWER",
"upperlimit": "QWER",
"startrange": "QWERT",
"endrange": "QWERT"
},
{
"lowerlimit": "QWER",
"upperlimit": "QWERT",
"startrange": "QWER",
"endrange": "QWERT"
},
{
"lowerlimit": "QWERT",
"upperlimit": "QWERT",
"startrange": "QWERT",
"endrange": "QWERT"
}
]
解决方案
这是一个有意冗长的解决方案,可能会有所帮助,因为您似乎正在学习一些关于 JavaScript 对象和数组的基础知识。
(这可以更简洁地完成Array.prototype.map
)
const
data = getOriginalData(),
firstItem = data[0],
// Creates a resultArray containing a single (currently empty) resultObject
resultObject = {},
resultArray = [resultObject],
// Specifies the three kinds of properties for the result object
propsToCopyFromFirstItem = ["fyyear", "kraid", "kraname"],
arrayProp = "kracriteria",
childProps = ["lowerlimit", "upperlimit", "startrange", "endrange"];
// Loops through propsToCopyFromFirstItem:
// Looks in the first item of `data` for the value to use and
// creates a property with that name and value in the result object
for (let propName of propsToCopyFromFirstItem){
let valueForProp = firstItem[propName];
resultObject[propName] = valueForProp;
}
// Creates a property whose value is an empty array to add items to
// Loops through `data`:
// Makes an empty `nextItem` obj (to add to the arrayProp in resultObject)
// Loops through `childProps`:
// Looks in the currentProperty in the currentObject of `data` for the value
// Makes a new prop in nextItem with that name and value
resultObject[arrayProp] = [];
for (let currentObject of data){
const nextItem = {};
for (let currentPropName of childProps){
let valueForProp = currentObject[currentPropName];
nextItem[currentPropName] = valueForProp;
}
// Adds `nextItem` to the array property in resultObject
resultObject[arrayProp].push(nextItem);
}
// Logs the result (`resultObject` was added to `resultArray` before)
console.log(resultArray);
// Provides the original `data` array
function getOriginalData(){
return [
{
"fyyear": "2019-2020",
"kraid": 5,
"kraname": "CSAT",
"kracriteria": 18,
"lowerlimit": 2,
"upperlimit": 3.99,
"startrange": 50,
"endrange": 60
},
{
"fyyear": "2019-2020",
"kraid": 5,
"kraname": "CSAT",
"kracriteria": 18,
"lowerlimit": 3,
"upperlimit": 5.99,
"startrange": 70,
"endrange": 80
}
];
}
推荐阅读
- image - 显示损坏的静态路线图像(Google Maps Directions API)
- nativescript - 自定义组件无法访问默认的 NativeScript 指令
- nested - Lisp:添加列表列表的各个元素
- vue.js - PhpStorm Vuetify 自动完成
- spring - Spring Framework 5.x 是否与 Oracle WebLogic 12c (12.2.1.3.0) 兼容?
- python - 装饰器的目的是什么(为什么要使用它们)?
- mysql - Codeigniter 连接的重复结果
- symfony - 信使rabbitmq symfony 4 ssl支持?
- sql - 时间序列图表数据的入口和出口点
- jquery - 数据表添加行