如何重组JSON对象以将其从一种形式转换为另一种形式。

huangapple go评论75阅读模式
英文:

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 = [{
  &quot;education&quot;: &quot;school&quot;,
  &quot;2021-12&quot;: 65.45,
  &quot;2022-01&quot;: 172762.27,
  &quot;2022-02&quot;: &quot;959.5&quot;
}, {
  &quot;education&quot;: &quot;colleg&quot;,
  &quot;2021-12&quot;: 270.13,
  &quot;2022-01&quot;: 105429534.09,
  &quot;2022-02&quot;: 53543770.9,
  &quot;2022-03&quot;: 100004545
}, {
  &quot;education&quot;: &quot;university&quot;,
  &quot;2021-12&quot;: 168396.95,
  &quot;2022-01&quot;: 170417648.96999997,
  &quot;2022-02&quot;: 1202822.9
}];

let result = [];
arr.forEach((element, index) =&gt; {
  let subObj = {}
  let keys = Object.keys(element);
  for (var propt in element) {
    if (keys.indexOf(propt) &gt; -1) {
      if (element.hasOwnProperty(&#39;2021-12&#39;)) {
        subObj[&#39;education&#39;] = 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 -->

[{
  &quot;school&quot;: &quot;65.45&quot;,
  &quot;colleg&quot;: &quot;270.13&quot;,
  &quot;university&quot;: &quot;168396.95&quot;,
  &quot;date&quot;: &quot;2021-12&quot;
}, {
  &quot;school&quot;: &quot;172762.&quot;,
  &quot;colleg&quot;: &quot;105429534.09&quot;,
  &quot;university&quot;: &quot;170417648.96999997&quot;,
  &quot;date&quot;: &quot;2022-01&quot;
}, {
  &quot;school&quot;: &quot;959.5&quot;,
  &quot;colleg&quot;: &quot;53543770.9&quot;,
  &quot;university&quot;: &quot;1202822.9&quot;,
  &quot;date&quot;: &quot;2022-02&quot;
}, {
  &quot;colleg&quot;: &quot;959.5&quot;,
  &quot;date&quot;: &quot;2022-03&quot;,
}]

<!-- 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 = [{
&quot;education&quot;: &quot;school&quot;,
&quot;2021-12&quot;: 65.45,
&quot;2022-01&quot;: 172762.27,
&quot;2022-02&quot;: &quot;959.5&quot;
}, {
&quot;education&quot;: &quot;colleg&quot;,
&quot;2021-12&quot;: 270.13,
&quot;2022-01&quot;: 105429534.09,
&quot;2022-02&quot;: 53543770.9,
&quot;2022-03&quot;: 100004545
}, {
&quot;education&quot;: &quot;university&quot;,
&quot;2021-12&quot;: 168396.95,
&quot;2022-01&quot;: 170417648.96999997,
&quot;2022-02&quot;: 1202822.9
}];
// Array for getting unique dates
let datesArray = [];
// Array for getting unique educations
let educationArray = [];
arr.forEach(item =&gt; {
for (key in item) {
if (key === &quot;education&quot;) {
// 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 =&gt; {
let educationForThisDate = {};
// Get educations for this date
educationArray.forEach(education =&gt; {
let educationValue = arr.filter(x =&gt; x[&#39;education&#39;] === education)[0][date];
// Check if education value exists for this date (we don&#39;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[&#39;date&#39;] = date;
finalResult.push(educationForThisDate);
});
console.log(finalResult);

<!-- end snippet -->

This can also be optimized a little bit, but for now it does the job.

huangapple
  • 本文由 发表于 2023年5月26日 16:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定