英文:
how to restructure json object to convert from one form to another form
问题
这是您想要的输出:
[{
"school": "65.45",
"colleg": "270.13",
"university": "168396.95",
"date": "2021-12"
}, {
"school": "172762.27",
"colleg": "105429534.09",
"university": "170417648.96999997",
"date": "2022-01"
}, {
"school": "959.5",
"colleg": "53543770.9",
"university": "1202822.9",
"date": "2022-02"
}, {
"colleg": "100004545",
"date": "2022-03"
}]
英文:
I have a scenario where my object has a date value as a key. I want to restructure the JSON so that every object has only one date.
I want the date value should assign to "education" and every object has only one date value.
The problem here is that my date is dynamic so I cant keep it as a key. The date can be variable. Each object should have one date and the value of the date should be assigned to education. For all the same date values then they should be in one object and the value of that date will be the value of the education.
This is the current data format and code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var arr = [{
"education": "school",
"2021-12": 65.45,
"2022-01": 172762.27,
"2022-02": "959.5"
}, {
"education": "colleg",
"2021-12": 270.13,
"2022-01": 105429534.09,
"2022-02": 53543770.9,
"2022-03": 100004545
}, {
"education": "university",
"2021-12": 168396.95,
"2022-01": 170417648.96999997,
"2022-02": 1202822.9
}];
let result = [];
arr.forEach((element, index) => {
let subObj = {}
let keys = Object.keys(element);
for (var propt in element) {
if (keys.indexOf(propt) > -1) {
if (element.hasOwnProperty('2021-12')) {
subObj['education'] = element[propt]
}
}
}
result.push(subObj)
});
console.log(result);
<!-- end snippet -->
This is the output I want:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
[{
"school": "65.45",
"colleg": "270.13",
"university": "168396.95",
"date": "2021-12"
}, {
"school": "172762.",
"colleg": "105429534.09",
"university": "170417648.96999997",
"date": "2022-01"
}, {
"school": "959.5",
"colleg": "53543770.9",
"university": "1202822.9",
"date": "2022-02"
}, {
"colleg": "959.5",
"date": "2022-03",
}]
<!-- end snippet -->
答案1
得分: 2
假设您的数组中始终存在“education”键,您可以像这样获取所需的数组:
var arr = [{
"education": "school",
"2021-12": 65.45,
"2022-01": 172762.27,
"2022-02": "959.5"
}, {
"education": "college",
"2021-12": 270.13,
"2022-01": 105429534.09,
"2022-02": 53543770.9,
"2022-03": 100004545
}, {
"education": "university",
"2021-12": 168396.95,
"2022-01": 170417648.96999997,
"2022-02": 1202822.9
}];
// 获取唯一日期的数组
let datesArray = [];
// 获取唯一教育类型的数组
let educationArray = [];
arr.forEach(item => {
for (key in item) {
if (key === "education") {
// 检查此教育类型是否已存在于数组中
if (!educationArray.includes(item[key])) {
educationArray.push(item[key]);
}
// 检查此日期是否已存在于数组中
} else if (!datesArray.includes(key)) {
datesArray.push(key);
}
}
});
// 用于存储最终结果的数组
let finalResult = [];
datesArray.forEach(date => {
let educationForThisDate = {};
// 获取此日期的教育类型
educationArray.forEach(education => {
let educationValue = arr.filter(x => x['education'] === education)[0][date];
// 检查是否存在此日期的教育数值(我们不希望追加数值为undefined的教育类型)
// 如果希望包括所有教育类型,无论它们是否为undefined或null,也可以删除此if检查
if (educationValue) {
educationForThisDate[education] = educationValue.toString();
}
});
educationForThisDate['date'] = date;
finalResult.push(educationForThisDate);
});
console.log(finalResult);
这段代码可以进行一些优化,但目前它可以完成任务。
英文:
Assuming that "education" key is present in your array all the time, you can get your desired array like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var arr = [{
"education": "school",
"2021-12": 65.45,
"2022-01": 172762.27,
"2022-02": "959.5"
}, {
"education": "colleg",
"2021-12": 270.13,
"2022-01": 105429534.09,
"2022-02": 53543770.9,
"2022-03": 100004545
}, {
"education": "university",
"2021-12": 168396.95,
"2022-01": 170417648.96999997,
"2022-02": 1202822.9
}];
// Array for getting unique dates
let datesArray = [];
// Array for getting unique educations
let educationArray = [];
arr.forEach(item => {
for (key in item) {
if (key === "education") {
// Check if this education already exists in array
if (!educationArray.includes(item[key])) {
educationArray.push(item[key]);
}
// Check if this date already exists in array
} else if (!datesArray.includes(key)) {
datesArray.push(key);
}
}
});
// Array for desired result
let finalResult = [];
datesArray.forEach(date => {
let educationForThisDate = {};
// Get educations for this date
educationArray.forEach(education => {
let educationValue = arr.filter(x => x['education'] === education)[0][date];
// Check if education value exists for this date (we don't want to append education with value undefined)
// Can also remove this if check if you want all educations regardless if they are undefined or null
if (educationValue) {
educationForThisDate[education] = educationValue.toString();
}
});
educationForThisDate['date'] = date;
finalResult.push(educationForThisDate);
});
console.log(finalResult);
<!-- end snippet -->
This can also be optimized a little bit, but for now it does the job.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论