0

I have an array of objects:

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}]

For each district I would like to take the values of paper and mgp for February, March, and April and divide each month's values by their January values to measure % change.

Using Array.findIndex() and reduce() (like here) I can match the districts and return the correct value, but that only returns the first index of the match, and there are repeat indices because there are subsequent months. I've tried wrapping a loop of months around the findIndex() to no avail: console.log(result) returns nothing

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
             }, []);

Result should look like this:

[{"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]
1
  • please add the wanted result. where are all moths gone? Commented Apr 29, 2020 at 19:48

3 Answers 3

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))

Sign up to request clarification or add additional context in comments.

Comments

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}];

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);

Comments

0

You could collect the january values first and then omit january in the result set.

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.