如何从给定的JSON对象数组中在JavaScript中进行数据透视。

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

How to Pivot the data in the javascript from given JSON array of objects

问题

你想要将给定的JSON对象数组按照三个变量作为索引列(分组列)、一个变量作为列,以及一个变量作为值进行数据透视。这在Python中可以使用Pandas轻松实现,但在JavaScript中该如何实现还不清楚。需要帮助,对吗?

期望的输出是将数据按照要求进行重新组织,以实现你所描述的效果。

英文:

How can I pivot the data from the given JSON array of objects by taking three variables as index columns(grouping columns), one variable as column and one variable as value.

Input Data is as follows :

const data = [
  {
    unitname: "A101",
    tenantname: "John Doe",
    tenantphonenumber: "1234567890",
    tenantemail: "johndoe@example.com",
    date: "JAN 2022",
    paidamount: 1000,
  },
{
    unitname: "A102",
    tenantname: "Jane Smith",
    tenantphonenumber: "0987654321",
    tenantemail: "janesmith@example.com",
    date: "JAN 2022",
    paidamount: 1200,
  },
 {
    unitname: "A103",
    tenantname: "Mark Smith",
    tenantphonenumber: "0987454321",
    tenantemail: "marksmith@example.com",
    date: "JAN 2022",
    paidamount: 1500,
  }
  {
    unitname: "A101",
    tenantname: "John Doe",
    tenantphonenumber: "1234567890",
    tenantemail: "johndoe@example.com",
    date: "FEB 2022",
    paidamount: 1000,
  },
  
  {
    unitname: "A102",
    tenantname: "Jane Smith",
    tenantphonenumber: "0987654321",
    tenantemail: "janesmith@example.com",
    date: "FEB 2022",
    paidamount: 1200,
  },
{
    unitname: "A103",
    tenantname: "Mark Smith",
    tenantphonenumber: "0987454321",
    tenantemail: "marksmith@example.com",
    date: "MAR 2022",
    paidamount: 1500,
  }
{
    unitname: "A101",
    tenantname: "John Doe",
    tenantphonenumber: "1234567890",
    tenantemail: "johndoe@example.com",
    date: "MAR 2022",
    paidamount: 1000,
  },
{
    unitname: "A102",
    tenantname: "Jane Smith",
    tenantphonenumber: "0987654321",
    tenantemail: "janesmith@example.com",
    date: "MAR 2022",
    paidamount: 1200,
  },
 {
    unitname: "A103",
    tenantname: "Mark Smith",
    tenantphonenumber: "0987454321",
    tenantemail: "marksmith@example.com",
    date: "MAR 2022",
    paidamount: 1500,
  }
{
    unitname: "A101",
    tenantname: "Mark Doe",
    tenantphonenumber: "093214567",
    tenantemail: "markdoe@example.com",
    date: "APR 2022",
    paidamount: 1000,
  },
{
    unitname: "A102",
    tenantname: "Jane Smith",
    tenantphonenumber: "0987654321",
    tenantemail: "janesmith@example.com",
    date: "APR 2022",
    paidamount: 1200,
  },
];

This is very simple to be achieved in Python by using Pandas, but unable to figure out how can we achieve in javascript.
Any help would be highly appreciated

Desired output :

const data = [
{
    unitname: "A101",
    tenantname: "John Doe",
    tenantphonenumber: "1234567890",
    tenantemail: "johndoe@example.com",
    "JAN 2022" : 1000,
    "FEB 2022" : 1000,
    "MAR 2022" : 1000
},
{
    unitname: "A102",
    tenantname: "Jane Smith",
    tenantphonenumber: "0987654321",
    tenantemail: "janesmith@example.com",
    "JAN 2022" : 1200,
    "FEB 2022" : 1200,
    "MAR 2022" : 1200,
    "APR 2022" : 1200
},
{
    unitname: "A103",
    tenantname: "Mark Smith",
    tenantphonenumber: "0987454321",
    tenantemail: "marksmith@example.com",
    "JAN 2022" : 1500,
    "FEB 2022" : 1500,
    "MAR 2022" : 1500
  
},
{
    unitname: "A101",
    tenantname: "Mark Doe",
    tenantphonenumber: "093214567",
    tenantemail: "markdoe@example.com",
    "APR 2022" : 1000
}
]

答案1

得分: 1

我们可以利用 reduce 根据 unitnumber 进行分组。

英文:

We could make use of reduce grouping by the unitnumber

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = [
  {
    unitname: &quot;A101&quot;,
    tenantname: &quot;John Doe&quot;,
    tenantphonenumber: &quot;1234567890&quot;,
    tenantemail: &quot;johndoe@example.com&quot;,
    date: &quot;JAN 2022&quot;,
    paidamount: 1000,
  },
{
    unitname: &quot;A102&quot;,
    tenantname: &quot;Jane Smith&quot;,
    tenantphonenumber: &quot;0987654321&quot;,
    tenantemail: &quot;janesmith@example.com&quot;,
    date: &quot;JAN 2022&quot;,
    paidamount: 1200,
  },
 {
    unitname: &quot;A103&quot;,
    tenantname: &quot;Mark Smith&quot;,
    tenantphonenumber: &quot;0987454321&quot;,
    tenantemail: &quot;marksmith@example.com&quot;,
    date: &quot;JAN 2022&quot;,
    paidamount: 1500,
  },
  {
    unitname: &quot;A101&quot;,
    tenantname: &quot;John Doe&quot;,
    tenantphonenumber: &quot;1234567890&quot;,
    tenantemail: &quot;johndoe@example.com&quot;,
    date: &quot;FEB 2022&quot;,
    paidamount: 1000,
  },
  
  {
    unitname: &quot;A102&quot;,
    tenantname: &quot;Jane Smith&quot;,
    tenantphonenumber: &quot;0987654321&quot;,
    tenantemail: &quot;janesmith@example.com&quot;,
    date: &quot;FEB 2022&quot;,
    paidamount: 1200,
  },
{
    unitname: &quot;A103&quot;,
    tenantname: &quot;Mark Smith&quot;,
    tenantphonenumber: &quot;0987454321&quot;,
    tenantemail: &quot;marksmith@example.com&quot;,
    date: &quot;MAR 2022&quot;,
    paidamount: 1500,
  },
{
    unitname: &quot;A101&quot;,
    tenantname: &quot;John Doe&quot;,
    tenantphonenumber: &quot;1234567890&quot;,
    tenantemail: &quot;johndoe@example.com&quot;,
    date: &quot;MAR 2022&quot;,
    paidamount: 1000,
  },
{
    unitname: &quot;A102&quot;,
    tenantname: &quot;Jane Smith&quot;,
    tenantphonenumber: &quot;0987654321&quot;,
    tenantemail: &quot;janesmith@example.com&quot;,
    date: &quot;MAR 2022&quot;,
    paidamount: 1200,
  },
 {
    unitname: &quot;A103&quot;,
    tenantname: &quot;Mark Smith&quot;,
    tenantphonenumber: &quot;0987454321&quot;,
    tenantemail: &quot;marksmith@example.com&quot;,
    date: &quot;MAR 2022&quot;,
    paidamount: 1500,
  },
{
    unitname: &quot;A101&quot;,
    tenantname: &quot;Mark Doe&quot;,
    tenantphonenumber: &quot;093214567&quot;,
    tenantemail: &quot;markdoe@example.com&quot;,
    date: &quot;APR 2022&quot;,
    paidamount: 1000,
  },
{
    unitname: &quot;A102&quot;,
    tenantname: &quot;Jane Smith&quot;,
    tenantphonenumber: &quot;0987654321&quot;,
    tenantemail: &quot;janesmith@example.com&quot;,
    date: &quot;APR 2022&quot;,
    paidamount: 1200,
  },
];


let w = data.reduce((x, y)=&gt; {
  y[y.date] = y.paidamount;
  delete y[&#39;date&#39;];
  delete y[&#39;paidamount&#39;];
  let key = y.unitname.concat(y.tenantemail); 
  if(!key in x) x[key] = y;
  else x[key] = {...x[key], ...y};
  return x;
}, {});

console.log(Object.values(w));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 23:10:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007070.html
匿名

发表评论

匿名网友

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

确定