How to show the number of users grouped by the month they signed up

Looking to showcase user counts based on the month they were created. Here's an example dataset:

[ 
  { name: 'user1', created: 'may' },
  { name: 'user2', created: 'may' },
  { name: 'user3', created: 'may' },
  { name: 'user4', created: 'may' },
  { name: 'user5', created: 'june' },
  { name: 'user6', created: 'june' },
  { name: 'user7', created: 'august' },
  { name: 'user8', created: 'august' },
  { name: 'user9', created: 'august' } 
]

The desired output should look like this:

may: 4
june: 2
august: 3

Any suggestions on how I can achieve this formatting?

Answer №1

To achieve this, one can utilize the reduce() method and output an object as the final result.

var data = [{"name":"user1","created":"may"},{"name":"user2","created":"may"},{"name":"user3","created":"may"},{"name":"user4","created":"may"},{"name":"user5","created":"june"},{"name":"user6","created":"june"},{"name":"user7","created":"august"},{"name":"user8","created":"august"},{"name":"user9","created":"august"}]

var result = data.reduce(function(r, e) {
  return r[e.created] = (r[e.created] || 0) + 1, r
}, {})

console.log(result)

Answer №2

Here is how you can get a sense of it:

let usersInMay = users.filter(user => user.created == 'may')

The variable usersInMay will either be null or an array. You can check its length to find out the number of users created in May.

usersInMay.length

You can iterate through all the months and determine the number of users created in each month.

Answer №3

One potential approach to consider is the following code snippet, however please note that it has not been tested so may require adjustments:

function extractData(month, dataset) {
       return let result = dataset.filter((entry)=>{
        return entry.created === month;
    })

Answer №4

If you're looking for a straightforward JavaScript solution, here's some code that can help:

var monthWithUsercount = {};

for (var i = 0; i < array.length; i++) {
    var number = array[i];
    monthWithUsercount[number.created] = monthWithUsercount[number.created] ? monthWithUsercount[number.created]+1 : 1;
}
//In this snippet, "array" holds the data with months and users.

console.log(monthWithUsercount);

You can also check out the live demo on JSFiddle by following this link - https://jsfiddle.net/sajalsuraj/7nychnd2/

Enjoy coding!

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

Adjusting the React Material UI TextField behavior upon a change in value while maintaining the

I have an MUI TextField component that I would like to trigger a function when it changes, while still allowing it to function as usual (without being a controlled input). <TextField onChange={(e)=>{ doSomething(e.target.value) //perhaps call ...

JavaScript: Comparing an array of arrays with multiple identical elements to another array and returning a unique array of arrays

const array1 = [[1, 2, 3], [1,3,4], [1,2,5]]; let b = [] let c = [2,3] array1.forEach(e => { c.some(r => { if(e.includes(r)) b.push(e) }) }) console.log(b) Upon running the code, the output was [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 3, ...

Ensure that the text field in vue.js restricts input to integers and cannot be left empty

Trying to implement an input field in vue.js that only accepts integer values, ensures the quantity is not 0, and defaults to 1 if left empty. I attempted to block decimals with the code: <input type="number" min="1" @keydown="filterKey"></input& ...

Can you suggest the optimal setup for (javascript SOAP)?

As part of my nightly maintenance process on a LAMP server, I need to retrieve data from a web service using SOAP, which is then applied to a database. While researching various options, I have become overwhelmed by the abundance of information available. ...

Changing the user object stored in the database within the next authentication process following registration

In my next.js application, I have implemented Next Auth for authentication and used a database strategy. Once a user logs in, is there a way to update their data? ...

Jest v29 Upgrade Issue: Test environment jest-environment-jsdom not found

Are there any success stories of upgrading to the newest version of Jest, specifically version 29? I keep encountering an error message: Error: Test environment jest-environment-jsdom cannot be found. Please ensure that the testEnvironment configuration ...

Error: It is not possible to add the "providers" property as the object is not extendable within N

Ever since updating to the latest version of NGRX, I've been encountering an issue: users$= createEffect(() => this.actions$ .pipe( ofType(users.LOAD_USERS), tap((action: users.LoadUsersAction) => { action.item.name = ...

Looking for an improved, tidier, or more efficient alternative to PHP Random Text?

Is there a more efficient way to generate random text other than using the random_text function in PHP? I'm interested in a method that is quick to render and light on server resources for faster page loading. Should I consider alternatives like Javas ...

Dealing with AJAX errors consistently in jQuery

Is it possible to efficiently handle 401 errors in AJAX calls and redirect to login.html without repeating the same code over and over again? if (xhr.status === 401) { location.assign('/login.html'); } I am seeking a way to manage these erro ...

Altering the texture of a mesh in Three.js can completely transform the appearance

I'm facing an issue with my model that contains multiple meshes. I only want to apply a texture to one specific mesh, but when I do, the entire model ends up with the same texture. What could be the mistake I'm making? function load_models(callb ...

Unable to successfully call a directive function from a nested child directive

I'm facing a challenge with utilizing a directive that was created by another developer to notify my directive when the ngRepeat inside of it has completed its creation. My parent directive has an isolate scope and includes a function within its scop ...

We encountered an issue: Headers cannot be set after they have been sent while attempting to read files

I encountered an error when attempting to read a file and send the response back to the browser. [ 'Error: Can\'t set headers after they are sent.', ' at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)&apo ...

The jQuery function for AJAX does not properly validate the boolean value provided by the controller

I have a controller action that returns a boolean result to jQuery. [HttpGet] public ActionResult IsVoucherValid(string voucherCode) { bool result = false; var voucher = new VoucherCode(voucherCode); if(voucher.Status==0) ...

Displaying only the final item from an array when clicking in an ng-repeat loop

My objective is to display the details of each value from my data in a pop-up window. However, instead of showing three different values, the pop-up only displays the last value from an array three times. This is the HTML code I am using: <div ng-re ...

What is the best method for enabling CORS policy when making a fetch request from a Vue.js frontend to

I am currently facing an issue with my front-end code that is making a request to the back end (frontend is running on localhost:8081 and sending a request to localhost:8080) This is the front-end code snippet: <script lang="ts">import &a ...

activating the submit button depending on the user input

My goal is to create a form with a textarea and a submit button that can be enabled or disabled based on whether there is any input in the textarea. I have confirmed that both the submit button and content are being selected correctly (I'll provide a ...

Having trouble connecting Nextjs with ChromaDB?

I am encountering issues while trying to establish a connection with the Chromadb vector database in Nextjs. The objective is to store user-generated content in Chromadb. Below is the code snippet I am utilizing along with its dependencies: Dependencies V ...

Utilize Firebase for seamless signup/login process leveraging web3 integration

Looking to implement a unique authentication method using web3 and Metamask in place of traditional email and password signups for Firebase. The challenge lies in handling the signup process - how can this be achieved? One potential solution is utilizing ...

Javascript initial keypress delay for movement in 3D space

I am aiming for a seamless movement experience when pressing a key - with the object initiating its movement upon the first press and then continuously moving while the button is held down. //setting movement speeds var xSpeed = 0.5; var zSpeed = 0.5; do ...

Fill in a text box with a chosen value from a linked drop-down menu

My database contains two tables: 1 - tbl_category 2 - tbl_shelf_place I am working towards displaying the shelf_code in a textbox based on the selected category_name from a drop-down (book_category) with a value of category_id, instead of using another ...