Tips for calculating the average of various items within an array

Hello everyone,

I'm currently facing a challenge in writing a JavaScript function that calculates the average values for an array of objects.

Below is the example array:

const array = [
      {
        createdAt: "2021-06-05",
        value: 0.2
      },
      {
        createdAt: "2021-06-05",
        value: 0.2
      },
      ...
    ]

The desired output should be:

const newArray = [
{
    createdAt: "2021-06-05",
    value: 0.467
  },
  ...
]

I attempted to use array.map on CodePen but encountered some issues, as shown in this example.

If you have any suggestions or solutions, I would greatly appreciate it!

Answer №1

Map function does not alter the length of the resulting array and therefore is not suitable for this task. You will need to utilize Reduce to restructure the data accordingly.

This problem can be broken down into 2 separate tasks:

  • Grouping items by a specific key (in this case, the creation date)
  • Calculating the average within each group

The code provided may not be the most concise, but it effectively clarifies the process and produces the desired results.

const array = JSON.parse(`[{\"createdAt\":\"2021-06-05\",\"value\":0.2},{\"createdAt\":\"2021-06-05\",\"value\":0.2},{\"createdAt\":\"2021-06-05\",\"value\":1},{\"createdAt\":\"2021-06-06\",\"value\":0.2},{\"createdAt\":\"2021-06-06\",\"value\":0.3},{\"createdAt\":\"2021-06-06\",\"value\":0.8},{\"createdAt\":\"2021-06-07\",\"value\":0.7},{\"createdAt\":\"2021-06-07\",\"value\":0.7},{\"createdAt\":\"2021-06-08\",\"value\":0.7},{\"createdAt\":\"2021-06-08\",\"value\":0.2},{\"createdAt\":\"2021-06-08\",\"value\":0.9}]`)


function calculateAverageByDate(data) {
  const groups = data.reduce((map, record) => {
    const { createdAt, value } = record;
    
    if (map.has(createdAt)) {
      map.set(createdAt, map.get(createdAt).concat(value));
    } else {
      map.set(createdAt, [value])
    }
    
    return map
  }, new Map())
  
  const averages = [...groups.entries()].map(([createdAt, values]) => {
    return {
      createdAt,
      value: values.reduce((a, b) => a + b)/values.length
    }
  })
  
  return averages
}

console.log(JSON.stringify(calculateAverageByDate(array)))

Answer №2

const data = [
  {
    date: "2021-06-05",
    amount: 0.2
  },
  {
    date: "2021-06-05",
    amount: 0.2
  },
  {
    date: "2021-06-05",
    amount: 1
  },
  {
    date: "2021-06-06",
    amount: 0.2
  },
  {
    date: "2021-06-06",
    amount: 0.3
  },
  {
    date: "2021-06-06",
    amount: 0.8
  },
  {
    date: "2021-06-07",
    amount: 0.7
  },
  {
    date: "2021-06-07",
    amount: 0.7
  },
  {
    date: "2021-06-08",
    amount: 0.7
  },
  {
    date: "2021-06-08",
    amount: 0.2
  }, 
  {
    date: "2021-06-08",
    amount: 0.9  
  }
]

let groupedData = data.reduce((grouped, {date, amount}) => ({
  ...grouped,
  [date]: grouped[date] 
                 ? [...grouped[date], amount] 
                 : [amount]
}), {})

let finalResult = Object.entries(groupedData).map(([date, amounts]) => ({
  date,
  amount: amounts.reduce((total, amount) => total + amount, 0) / amounts.length
}))

Answer №3

To start, create an object where the keys represent dates and the values are arrays of accumulated values for that date.

Utilize Object.entries along with map to generate an array of objects with average calculations.

const calculateAverage = (arr) => {
  const allValuesByDate = {};
  arr.forEach(({ createdAt, value }) => (allValuesByDate[createdAt] ??= []).push(value));
  return Object.entries(allValuesByDate).map(([createdAt, values]) => ({
    createdAt,
    avgValue: values.reduce((sum, val) => sum + val) / values.length,
  }));
};

const dataArray = [
  { createdAt: "2021-06-05", value: 0.2 },
  { createdAt: "2021-06-05", value: 0.2 },
  { createdAt: "2021-06-05", value: 1 },
  { createdAt: "2021-06-06", value: 0.2 },
  { createdAt: "2021-06-06", value: 0.3 },
  { createdAt: "2021-06-06", value: 0.8 },
  { createdAt: "2021-06-07", value: 0.7 },
  { createdAt: "2021-06-07", value: 0.7 },
  { createdAt: "2021-06-08", value: 0.7 },
  { createdAt: "2021-06-08", value: 0.2 },
  { createdAt: "2021-06-08", value: 0.9 },
];


console.log(calculateAverage(dataArray));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach to resolving the MongoServerError: E11000 duplicate key error?

Index.Js File: const cookieSession = require("cookie-session"); const express = require("express"); const app = express(); const helmet = require("helmet"); const morgan = require("morgan"); const dotenv = require(&q ...

How can I trigger a series of functions in sequence when a button is clicked using Vue.js?

When I click, I need to call 3 functions in a specific order: <CButton @click=" getBillPrice(); updateBill(); postData();" type="submit" color="primary">Save</CButton> However, the func ...

What steps should I take to incorporate this feature into my Meteor project?

After successfully installing the type.js package, I discovered that the code works fine when directly executed in the console. Here is the code snippet: $(function(){ $(".typedelement").typed({ strings: ["You don&apo ...

A guide on invoking a JavaScript function within a dropdown menu based on selection instead of change event

I need to automatically trigger a JavaScript function based on the value pulled from the dropdown options that are populated by a database. Currently, the JavaScript function only runs when I manually select an option on the front-end. Below is my code. I ...

Regex produces odd results in string processing

This particular JavaScript regular expression: homework.description = (homework.input.match(/((\(((\S\s?)\)?)*)|(about( \w*)*))/i)); When applied to the text: potato (potato) Produces this unexpected output: (potato),(potato), ...

Shift all content to the right when viewing on mobile devices

I'm looking to create space for a menu on the left side, similar to the one on Mashable's mobile view. How can I move all the content to the right? Feel free to resize the window and compare with . Thank you. Best regards, Marius ...

Ways to guide users through a single-page website using the URL bar

I currently have a one-page website with links like <a href="#block1">link1</a>. When clicked, the browser address bar displays site.com/#block1 I am looking to modify this so that the browser shows site.com/block1, and when the link is clicke ...

I encountered an SyntaxError while working with Ionic. The error message reads: "Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)."

Here's the code that is causing me trouble: this.http.get('http://localhost/....) .map((res) => res.json()) .subscribe(( this.navCtrl.push(OtpPage,{mobileno:this.mobile}); }, (err) => { console.log(err ...

What is the best method to utilize a promise to delay the execution of a function until the data is received and stored

Currently, I am facing an issue with my API where the model variable is returning undefined before any data is populated in the return_array. I am unsure of how to implement promises or another method to ensure that the variable waits for data to be fille ...

Unable to upload gathered email to Mailchimp mailing list through Nodejs and express API

Seeking guidance on integrating data collection with Mailchimp in a Nodejs backend project. I am currently working on an email signup list page where users input their first name, last name, and email. The HTML file contains fields named firstName, lastN ...

Is it possible to access variables within functions from outside of them?

async function checkPlayersOnline() { const response = await fetch("http://ip:port/dynamic.json"); const playersData = await response.json(); console.log(playersData.clients); } Is it possible to access the playersData inside another func ...

Tips for combining or removing letters from the existing content in an input field for editing todo items in Redux

At this moment, my focus is on implementing an edit feature for my to-do app. The issue I am facing is that it manages to retrieve the current value, but there seems to be a restriction on deleting or adding letters. You can refer to the first input field ...

Ways to eliminate flickering when dynamically updating iframe content

Currently, I am in the process of constructing an iframe slideshow that consists of 7 webpages named event1.html to event7.html. To implement the automatic changing of the iframe source every 1 second, I am utilizing setInterval. However, I am facing an is ...

Using axios to make a request from a server to itself

I'm facing an issue where I am attempting to send a request from the server to the same server using axios as a PUT method. Here is an example of what I have tried: await axios({ url: `http://localhost:4000${url}`, method: requestType, ...

Vue2 is not compatible with the vue-email-editor component

I checked out the official website to install the vue-email-editor. Here is the link for the unlayer vue-email-editor component However, I encountered the following error: vue.runtime.esm.js?c320:4573 [Vue warn]: Error in render: "TypeError: (0 , ...

Is it possible to conceal JavaScript comments on a page before it is displayed?

Curiosity has led me to ponder a question that may seem trivial. Consider this scenario: in my HTML or ASPX page, I include a comment for some JavaScript code. When the page loads, will the comments be rendered along with the rest of the page's conten ...

Redirecting script upon successful connection detection

I have created a script that checks for internet connectivity using an image, and redirects if internet is available. However, the issue is that it caches the images, leading to attempts to load them even when offline. Is there a different approach I can ...

Utilizing Angular 11's HostListener to capture click events and retrieve the value of an object

Using the HostListener directive, I am listening for the click event on elements of the DOM. @HostListener('click', ['$event.target']) onClick(e) { console.log("event", e) } Upon clicking a button tag, the "e" object contains the fol ...

HTML View of JSON Object Hierarchy

Hello, I have been trying various methods but am struggling to figure out how to display the following JSON object as a tree HTML view with ul li items using JavaScript. Can anyone help me with this? [ { "CategoriesModelId": 7, "Name": "Parent ...

"Implementing a column template in jqgrid post-creation: A step-by-step

Can't apply column template with Free jqgrid once it's been created. I've attempted: var newOrderPriceTemplate = { align: "center", formatter: "showlink", formatoptions: { onClick: function() { alert('clicked&apos ...