Implementing JavaScript's reduce method to calculate the percentage of an element's presence within an array

I am attempting to utilize the reduce method to generate an object that displays the percentage of occurrence of different countries in a given list.

Input:

countriesList = ["US","US","US","UK","IT","IT"]

Desired Output:

percCountriesList = [{"country": "US", "weight": 0.5}, {"country": "UK", "weight": 0.1666}, {"country": "IT", "weight": 0.3333}]

Method for calculating percentages:

const countriesList = ["US","US","US","UK","IT","IT"]
const weightPercCountries = countriesList.reduce((pcts, x) => {
    pcts[x] = (pcts, (pcts[x] ? pcts[x] : 0) + 100 / countriesList.length);
    return pcts;
}, []);
console.log(weightPercCountries)

Having obtained the list of percentages:

[50, 16.666666666666668, 33.33333333...]

Now, how can I format the desired output (country + weight) in JSON format? Appreciate any help!

Answer №1

Initially, the code generates an empty array with only a few additional properties. This occurs because the variable x is used as a country abbreviation rather than an index. Therefore, assigning values like pcts[x] = .. equates to something along the lines of pcts['us'] = ..., which may not be logical for an array in most scenarios.

If you desire to include a complex object within the array, it must be created separately. Consider the example below:

  1. I have set the reduce function to return an object, facilitating easy verification if the current country (denoted by x) already exists within the data structure.

  2. If the country is absent, I introduce a new property to the object containing all necessary attributes in the outcome (e.g., { country: x, percentage: 0}).

  3. After ensuring the existence of an object for the specific country, I can modify the percentage value by referencing its name. You can choose between 50 or 0.5 depending on your preference, achieved through calculations like 100/countriesList.length or 1/countriesList.length.

  4. The revised reduce function now produces an object structured similar to:

     {
       "us": { country: "us", percentage: 0.5},
       "it": { country: "it", percentage: 0.33} 
       ...
     }
    

    To retrieve an array of these values, utilize Object.values(...) which converts enumerable object properties into an array format.

const 
  countriesList = ["US","US","US","UK","IT","IT"];
  

const 
  weightPercCountries = Object.values(countriesList.reduce((pcts, x) => {
    if (!(x in pcts))
      pcts[x] = { country: x, percentage: 0}
    
    pcts[x].percentage += (1 / countriesList.length);
    return pcts;
}, {}));

console.log(weightPercCountries)

It is possible to condense the callback of the reduce function (as seen in Andrew Park's response). However, to enhance readability—especially for beginners—I opted for a more descriptive approach in the code...

Answer №2

let countriesArray = ["US","US","US","UK","IT","IT"]

const result = Object.values(countriesArray.reduce((accumulator, currentValue, index, array) => {
  ((accumulator[currentValue] ??= {'country':currentValue, weight:0}).weight += 1/array.length, accumulator)
}, {}))

console.log(result)

This code snippet demonstrates the use of the ??= operator to assign values when a property is not defined. Additionally, it utilizes comma expressions to streamline the logic without requiring explicit return statements.

An expanded version of the code can be written as:

let countriesArray = ["US","US","US","UK","IT","IT"]

const mappedResult = countriesArray.reduce((accumulator, currentValue, index, array) => {
  if (!accumulator[currentValue]) {
    accumulator[currentValue] = {'country': currentValue, weight: 0}
  }
  accumulator[currentValue].weight += 1 / array.length
  return accumulator
}, {})

// Output:
// {
//   US: { country: 'US', weight: 0.5 },
//   UK: { country: 'UK', weight: 0.16666666666666666 },
//   IT: { country: 'IT', weight: 0.3333333333333333 }
// }

// Extract only the object values, excluding keys
const finalResult = Object.values(mappedResult)

console.log(finalResult)

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

After successfully receiving my complex JSON data, the application encountered a malfunction when attempting to display the data in my listView layout

Currently, I am developing an Android application using Android Studio and Retrofit with the goal of calling an API that returns data in JSON format. In the JSON structure, the root element is a JsonObject that contains nested elements like this: { "count ...

`Proliferating values through constantly changing addition`

I am facing an issue with my code that involves 3 input fields: <div class="row"> <input onblur="calc_basic_amount();" id="rate_basic"></input> <input onblur="calc_basic_amount();" id="qty_b ...

What would be the best TypeScript target and libs to utilize in a transpiler-free Node.js project?

If I am running a TypeScript Node.js project with the command tsc && node build/index.js (where tsc builds into build/ and index.ts is in the project), what values should be used in lib and target in tsconfig.json to ensure access to the latest TypeScrip ...

VueJS failing to pass parent data to child component

I'm fairly new to Vue Framework and I'm trying to figure out how to update a child component based on changes in the parent component's attributes. In the code snippet below, I've created a component that displays a greeting message bas ...

The JSON.stringify() method is overwriting the file object with a blank one

COMPLEXITY Hey everyone, I could really use some assistance in resolving this intricate problem. I am working on an application using a combination of Spring Boot v2.0.5 and React.js v15.6.2, ReactDom v15.6.2, React Bootstrap v0.32.4. The communication be ...

ReactJS: Error message indicating that the update depth limit has been exceeded

I'm facing an issue with toggling the state of a component in ReactJS. The error message I am receiving says: Maximum update depth exceeded. This can occur when a component repeatedly calls setState within componentWillUpdate or componentDidUpdate. ...

Running multiple JavaScript servers simultaneously can be achieved by utilizing specific tools and

Currently, I am working on developing a Discord bot and have encountered some issues along the way that all required the same solution. The fix involved running separate batch files instead of running everything in my main file (index.js). I opted to use n ...

Setting up TypeScript in an Angular 2 project and integrating Facebook login

Currently, I am in the process of familiarizing myself with Angular 2 and typescript. Although things have been going smoothly so far, I have hit a roadblock while attempting to implement a Facebook login message. In my search for a solution, I stumbled up ...

Loading a javascript file in an asynchronous manner

Attempting to perform an asynchronous call to a server using the following method: $(document).ready(function(){ $.ajax({ cache: true, async: true, dataType: "script", url:"www.xyz.com/yyy?host_name=abc.com&amp;size=S&a ...

"Experience the power of using Selenium with Node.js in an asynchronous function

I'm currently developing a simple program that retrieves the titles of all the links when conducting a search on Google using Selenium Take a look at the code below: const {Builder,By, Key, until} = require('selenium-webdriver'); driver = ...

Traverse each child element sequentially using setTimeout or a delay function

Looking to dynamically apply a CSS filter to a list of divs with a delay between each iteration. Here are my attempted solutions: $(this).children().each(function() { $(this).delay(5000).css("-webkit-filter", "brightness(2)"); }); And this alternativ ...

Text in d3.js vanishing while undergoing rotation

I have been struggling for hours with what seems like a simple problem and haven't made any progress. I'm hoping to receive some valuable advice from the brilliant minds on stackoverflow. You can view my demo at I attempted to use jsfiddle to s ...

Converting a C# two-dimensional array into JavaScript formatting

Is there a more streamlined method for converting a two-dimensional array in C# like [[1, 2, 3],[4, 5, 6]] into a string representation "[[1, 2, 3],[4, 5, 6]]" without manually iterating through each value and formatting it? I want to pass the array as an ...

Issue with Axios fetching data with parameter in Next.js not resolving

While working with Next.js, I encountered an issue where the input text value (email) is successfully displayed in the console, but when trying to use this value as a parameter, "{emails}" is being saved in the database instead of the dynamic email value. ...

Exploring the contents of an array in ReactJS

const rowData = this.state.market.map((market) => { console.log("details", market["info"]) { return { marketInfo: ( <div> {market && !!market["info"] ? ( <div> ...

Get Angular events in the proper order

I am currently facing challenges with event handling in AngularJs. In my service, I send out events using the following line of code : $rootScope.$emit("FNH."+this.status, flowNodeHelper); When receiving the event in service "S1," I handle it as follows ...

What is the best approach for managing Promise rejections in Jest test scenarios?

Currently, I am engaged in a node JS project where my task is to write test cases. Below is the code snippet that I am working on - jest.mock('../../utils/db2.js') const request = require('supertest') const executeDb2Query = require(&ap ...

Display varying information depending on the option class, rather than its value

On my website, I have multiple forms structured like this: <form action="" method="post"> <input type="text"> <select name="a"> <option class="hide-all">Choose</option> <option class="show-link">Show link</option ...

Navigating through JSON data in an Angular application

I am currently facing an issue with uploading and parsing a JSON file in my Angular application. The problem lies in the fact that even though I can successfully upload the file, I am unable to access the data from it. To ensure the correct file is being ...

Positioning an HTML table with the use of a for loop in JavaScript

Detail of the project: -Utilizing a javascript for loop, my program extracts data from an external javascript array titled "arrays.js" and organizes it into an HTML table. The goal is to align the appropriate data under the "Date Name Address Amount" colum ...