Home > Enterprise >  Filter and calculate the properties of objects from the same table using javascript
Filter and calculate the properties of objects from the same table using javascript

Time:12-16

I hope my question is not asked too much in SO.

I have an array of 3 objects such as: :

const objStart1 = {
    'user': 1,
    'score': 15,
    'date': 'Monday'
}

const objStart2 = {
    'user': 1,
    'score': 7,
    'date': 'Friday'
}

const objStart3 = {
    'user': 2,
    'score': 5,
    'date': 'Monday'
}

I would like to group by date then calcul the average of the score by day (and delete user) and return a new array of objects like :

 const objStart1 = {
    'average': 10,
    'date': 'Monday'
}

const objStart2 = {
    'average': 7,
    'date': 'Friday'
}

It seems, we can use filter reduce but my results are pathetic... Is there a good solution?

thank you in advance.

CodePudding user response:

This should do the trick. I put your items into an actual array, and I replaced Date with date.

const input = [
  {
    'user': 1,
    'score': 15,
    'date': 'Monday'
  },
  {
    'user': 1,
    'score': 7,
    'date': 'Friday'
  },
  {
    'user': 2,
    'score': 5,
    'date': 'Monday'
  },
]

const sumList = input.reduce((acc, current) => {
    if(!acc.find(obj => obj.date === current.date)) acc.push({date: current.date, sum: 0, count: 0})
    const currCounter = acc.find(obj => obj.date === current.date)
    currCounter.sum  = current.score
    currCounter.count  
    return acc
  }, [])
  .map(({date, sum, count}) => ({ date, average: sum/count  }))

console.log(sumList)

CodePudding user response:

const objStart1 = {
    'user': 1,
    'score': 15,
    'date': 'Monday'
}

const objStart2 = {
    'user': 1,
    'score': 7,
    'date': 'Friday'
}

const objStart3 = {
    'user': 2,
    'score': 5,
    'date': 'Monday'
}

const objs = [objStart1, objStart2, objStart3];
let avgForDate = {};
avgForDate = objs.reduce((res, current) => {
  const oldInfo = res[current.date] ?? {
    count: 0,
    scoreSum: 0,
  };
  const newInfo = {
    count: oldInfo.count   1,
    scoreSum: oldInfo.scoreSum   current.score,
  };
  res[current.date] = newInfo;
  return res;
}, avgForDate);

for(const info in avgForDate) {
  console.log({ 
    date: info,
    avg: avgForDate[info].scoreSum / avgForDate[info].count,
  });
}

CodePudding user response:

You can do the following:

(Please note I changed the key Date to date).

function getAverageScoreByDate(...items) {
  const dates = items.reduce((previousValue, currentValue) => {
    if(previousValue.includes(currentValue.date)) {
      return previousValue
    };

    return [
      ...previousValue,
      currentValue.date,
    ]
  }, []);

  const averageScoresByDate = dates.map(date => {
    const itemsByDate = items.filter(item => item.date === date);
    const average = itemsByDate.reduce((previousValue, currentValue) => previousValue  = currentValue.score, 0);

    return {
      date,
      average
    }
  });

  return averageScoresByDate;
};

getAverageScoreByDate(objStart1, objStart2, objStart3);

It'll allow for any amount of args if you have more than 3 objects.

I've separated some bits out for the purposes of the demo but it's possible to chain Array methods.

CodePudding user response:

Another way of writing it.

const arr=[ { 'user': 1, 'score': 15, 'date': 'Monday' }, { 'user': 1, 'score': 7, 'date': 'Friday' }, { 'user': 2, 'score': 5, 'date': 'Monday' } ]
  
  const result = arr.reduce((acc, curr, i)=>{
    if(!acc.find((el)=>el.date==curr.date)){
        acc.push({date: curr.date, score: curr.score, count: 1}) 
        return acc
    }
    acc.map(el=>el.date===curr.date?(el.score =curr.score,el.count =1,el):el)
    return acc
  },[])

  console.log(result.map((el)=>({average: el.score/el.count, date: el.date})))

  • Related