我有一个对象数组:
var data = [{"district":"201","date":"Wed Apr 01 2020","paper":671.24,"mgp":36.5},
{"district":"202","date":"Wed Apr 01 2020","paper":421.89,"mgp":44.2},
{"district":"203","date":"Wed Apr 01 2020","paper":607.85,"mgp":67.36},
{"district":"201","date":"Sun Mar 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sun Mar 01 2020","paper":421.89,"mgp":36.6},
{"district":"203","date":"Sun Mar 01 2020","paper":607.85,"mgp":69.36},
{"district":"201","date":"Sat Feb 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sat Feb 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Sat Feb 01 2020","paper":607.85,"mgp":59.66},
{"district":"201","date":"Wed Jan 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Wed Jan 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Wed Jan 01 2020","paper":607.85,"mgp":89.26}]
[对于每个district
,我想获取2月,3月和4月的paper
和mgp
的值,然后将每个月的值除以其1月的值以测量变化百分比。
使用Array.findIndex()和reduce()(例如here),我可以匹配区域并返回正确的值,但是那只会返回匹配的第一个索引,并且由于存在后续月份,所以存在重复索引。我试过绕着findIndex()绕了几个月的循环无济于事:console.log(result)
不返回任何内容
var result = data.reduce((a, b) => {
var months = [...new Set(a.map(m => a.date))];
for (var month of months){
var idx = a.findIndex(e => e.district == b.district && e.date == month)
if (~idx && b.date === "Wed Jan 01 2020") {
a[idx].paper = a[idx].paper/b.paper;
a[idx].mgp = a[idx].mgp/b.mgp}
else {
a.push(JSON.parse(JSON.stringify(b)));
}
return a
}, []);
结果应如下所示:
[{"district":"201","date":"Wed Apr 01 2020","paper":1.17,"mgp":0.94},
{"district":"202","date":"Wed Apr 01 2020","paper":1,"mgp":1.99},
{"district":"203","date":"Wed Apr 01 2020","paper":1,"mgp":0.75},
{"district":"201","date":"Sun Mar 01 2020","paper":1,"mgp":1},
{"district":"202","date":"Sun Mar 01 2020","paper":1,"mgp":1.64},
{"district":"203","date":"Sun Mar 01 2020","paper":1,"mgp":0.77},
{"district":"201","date":"Sat Feb 01 2020","paper":1.17,"mgp":1},
{"district":"202","date":"Sat Feb 01 2020","paper":1,"mgp":1},
{"district":"203","date":"Sat Feb 01 2020","paper":1,"mgp":0.67]
var data = [{"district":"201","date":"Wed Apr 01 2020","paper":671.24,"mgp":36.5},
{"district":"202","date":"Wed Apr 01 2020","paper":421.89,"mgp":44.2},
{"district":"203","date":"Wed Apr 01 2020","paper":607.85,"mgp":67.36},
{"district":"201","date":"Sun Mar 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sun Mar 01 2020","paper":421.89,"mgp":36.6},
{"district":"203","date":"Sun Mar 01 2020","paper":607.85,"mgp":69.36},
{"district":"201","date":"Sat Feb 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sat Feb 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Sat Feb 01 2020","paper":607.85,"mgp":59.66},
{"district":"201","date":"Wed Jan 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Wed Jan 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Wed Jan 01 2020","paper":607.85,"mgp":89.26}];
const byDistrict = {};
for (const item of data) {
const month = item.date.split(' ')[1]; // eg Apr
if (!byDistrict[item.district]) byDistrict[item.district] = { };
byDistrict[item.district][month] = { paper: item.paper, mgp: item.mgp, date: item.date };
}
for (const district of Object.keys(byDistrict)) {
const District = byDistrict[district];
District.all = {
district,
paper: (District.Feb.paper + District.Mar.paper + District.Apr.paper) / District.Jan.paper,
mgp: (District.Feb.mgp + District.Mar.mgp + District.Apr.mgp) / District.Jan.mgp
}
}
const result = Object.keys(byDistrict).map(it => byDistrict[it].all);
console.log(result);
var data = [{ district: "201", date: "Wed Apr 01 2020", paper: 671.24, mgp: 36.5 }, { district: "202", date: "Wed Apr 01 2020", paper: 421.89, mgp: 44.2 }, { district: "203", date: "Wed Apr 01 2020", paper: 607.85, mgp: 67.36 }, { district: "201", date: "Sun Mar 01 2020", paper: 571.24, mgp: 38.8 }, { district: "202", date: "Sun Mar 01 2020", paper: 421.89, mgp: 36.6 }, { district: "203", date: "Sun Mar 01 2020", paper: 607.85, mgp: 69.36 }, { district: "201", date: "Sat Feb 01 2020", paper: 571.24, mgp: 38.8 }, { district: "202", date: "Sat Feb 01 2020", paper: 421.89, mgp: 22.2 }, { district: "203", date: "Sat Feb 01 2020", paper: 607.85, mgp: 59.66 }, { district: "201", date: "Wed Jan 01 2020", paper: 571.24, mgp: 38.8 }, { district: "202", date: "Wed Jan 01 2020", paper: 421.89, mgp: 22.2 }, { district: "203", date: "Wed Jan 01 2020", paper: 607.85, mgp: 89.26 }],
january = data.reduce((r, o) => {
if (o.date.includes('Jan')) r[o.district] = o;
return r;
}, {}),
result = data.reduce((r, o) => {
if (o.date.includes('Jan')) return r;
r.push({
...o,
paper: +(o.paper / january[o.district].paper).toFixed(2),
mgp: +(o.mgp / january[o.district].mgp).toFixed(2)
})
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var data = [{"district":"201","date":"Wed Apr 01 2020","paper":671.24,"mgp":36.5},
{"district":"202","date":"Wed Apr 01 2020","paper":421.89,"mgp":44.2},
{"district":"203","date":"Wed Apr 01 2020","paper":607.85,"mgp":67.36},
{"district":"201","date":"Sun Mar 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sun Mar 01 2020","paper":421.89,"mgp":36.6},
{"district":"203","date":"Sun Mar 01 2020","paper":607.85,"mgp":69.36},
{"district":"201","date":"Sat Feb 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sat Feb 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Sat Feb 01 2020","paper":607.85,"mgp":59.66},
{"district":"201","date":"Wed Jan 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Wed Jan 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Wed Jan 01 2020","paper":607.85,"mgp":89.26}]
let res = data.filter(it => !it.date.includes('Jan'))
.reduce ((acc, rec) => [...acc, {district: rec.district, date: rec.date,
paper: +(rec.paper/data.filter(it => (it.district === rec.district && it.date.includes('Jan')))[0].paper).toFixed(2),
mgp: +(rec.mgp/data.filter(it => (it.district === rec.district && it.date.includes('Jan')))[0].mgp).toFixed(2)}]
,[])
console.log(JSON.stringify(res))