Home Web Front-end JS Tutorial How do you group JavaScript objects by multiple properties and sum their values?

How do you group JavaScript objects by multiple properties and sum their values?

Nov 11, 2024 am 03:30 AM

How do you group JavaScript objects by multiple properties and sum their values?

Grouping Objects By Multiple Properties and Summing Their Values

In JavaScript, it's common to work with arrays of objects. Sometimes, it's necessary to group these objects by multiple properties while performing calculations on their values, such as summing up specific properties.

Problem Overview

Given an array of objects, we aim to group them based on two properties (shape and color). However, we want to treat objects as duplicates only if their shape and color match. The goal is to sum up the used and instances properties of objects within each group and remove any duplicates.

Expected Result

Using the following sample array:

1

2

3

4

5

6

7

8

9

10

arr = [

    {shape: 'square', color: 'red', used: 1, instances: 1},

    {shape: 'square', color: 'red', used: 2, instances: 1},

    {shape: 'circle', color: 'blue', used: 0, instances: 0},

    {shape: 'square', color: 'blue', used: 4, instances: 4},

    {shape: 'circle', color: 'red', used: 1, instances: 1},

    {shape: 'circle', color: 'red', used: 1, instances: 0},

    {shape: 'square', color: 'blue', used: 4, instances: 5},

    {shape: 'square', color: 'red', used: 2, instances: 1}

];

Copy after login

We expect to obtain an array with four groups:

1

2

3

4

[{shape: "square", color: "red", used: 5, instances: 3},

{shape: "circle", color: "red", used: 2, instances: 1},

{shape: "square", color: "blue", used: 11, instances: 9},

{shape: "circle", color: "blue", used: 0, instances: 0}]

Copy after login

Solution Using Array#reduce and Object#assign

To achieve this, we can utilize JavaScript's Array#reduce method to iterate over the array of objects. For each object, we construct a unique key by concatenating the shape and color properties. We then use an auxiliary object, helper, to keep track of the grouped objects.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

var helper = {};

var result = arr.reduce(function(r, o) {

  var key = o.shape + '-' + o.color;

 

  if(!helper[key]) {

    helper[key] = Object.assign({}, o); // create a copy of o

    r.push(helper[key]);

  } else {

    helper[key].used += o.used;

    helper[key].instances += o.instances;

  }

 

  return r;

}, []);

Copy after login

If the key does not exist in the helper object, we add a new entry using Object.assign() to create a fresh copy of the current object. We then push this new object into the result array.

If the key already exists in helper, it means we've encountered a duplicate. In this case, we simply increment the used and instances properties of the corresponding object in helper.

Finally, we return the result array, which effectively groups objects by shape and color while summing their values.

The above is the detailed content of How do you group JavaScript objects by multiple properties and sum their values?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles