英文:
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: "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,
},
];
let w = data.reduce((x, y)=> {
y[y.date] = y.paidamount;
delete y['date'];
delete y['paidamount'];
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论