Combining objects in a JavaScript array based on a specific property

Here is an array I have:

var array = [
      { category: 'Input', field: 0, value: '17' },
      { category: 'Input', field: 0, value: '5' },
      { category: 'Input', field: 0, value: '8' },
      { category: 'Input', field: 5, value: '1' },
      { category: 'Input', field: 5, value: '4' },
      { category: 'Input', field: 0, value: '1' }
    ];
    

I am looking to transform the array as shown below -- merging objects based on their field values if they are the same and obtaining a result like this:

[
      { category: 'Input', field: 0, value: ['17', '5', '8', '1'] },
      { category: 'Input', field: 5, value: ['1', '4'] }
    ];
    

Answer №1

let data = [
  { type: 'Number', amount: 5, result: 'OK' },
  { type: 'String', amount: 10, result: 'Fail' },
  { type: 'Boolean', amount: 3, result: 'Error' }
];

const summary = data.reduce((acc, curr) => {
  if (acc[curr.type]) {
    acc[curr.type]++;
  } else {
    acc[curr.type] = 1;
  }
  return acc;
}, {});

const output = Object.keys(summary).map(key => ({
  type: key,
  count: summary[key]
}));

console.log(output);

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

Does merging two text arrays in Python result in adding a "u"?

For anyone experienced in Python, this should be an easy question to answer (unfortunately, I'm not one of them). I am currently using Python 2.7.9 and came across some sample code on a website: When running the module, instead of getting a ping time ...

An issue arose with ReactDom during the development of the application

I am currently working on my final assignment for the semester, where I have created a project and finished coding it. However, when I try to load the localhost, the page remains blank even though the console in vsc shows no errors. Upon checking the conso ...

Can an integer-like object with the ability to store instance variables be created in Python?

Can Python support a custom data object that acts like a typical integer during math operations and comparisons while also having the ability to hold instance variables? In essence, it should allow for the following functionality: pseudo_integer = Pseudo ...

Place the text (menu) adjacent to the navigation bar

I am currently using bootsrap 4 alpha 6 in combination with midnight.js to alter the color of my navigation menu toggler. I am trying to incorporate a text element (MENU) alongside it, similar to the example shown in the Capture image. For the text toggler ...

Occasionally, there are instances when node.js combined with express.js and hogan.js may result in a blank page being

Most of the time, every feature in this app functions properly. However, there are instances when a blank page is displayed instead of the homepage (or any other page). Interestingly, making a minor adjustment to the affected view, such as adding a space o ...

Custom font not displaying on Chromecast receiver app

I have followed the specified steps to incorporate a custom font into an html canvas text field. Interestingly, the font displays correctly when accessed on the Desktop Chrome browser, but on the Chromecast receiver application, the font fails to load. Wha ...

Capture Your Scene with Three.js

I am facing an issue where I need to capture a screenshot of a website. I have tried using html2canvas and it is working fine. However, the problem arises because I am using both THREE.WebGLRenderer and THREE.CSS3DRenderer (for HTML in webGL). The screen ...

Having trouble accessing the value of an <asp:HiddenField> using javascript

Currently, I am setting a value to a hidden field and attempting to retrieve that value in my JavaScript code. In the declarative part of my code: <asp:HiddenField ID="chkImages" runat="server" /> <div id="main" runat="server" style="display:non ...

The Eclipse IDE is experiencing issues with the functionality of the JavaScript Number class

In my latest project, I decided to create a Dynamic Web Project using the Eclipse IDE 2019-12 along with Apache Tomcat 8.5.55 server. The main functionality of this project is that a user can input integers and receive their sum as an output. To ensure dat ...

How can you extract a string from an array?

When attempting to loop through a string array in TypeScript, I encounter an error stating that foreach is not a function. What is the correct way to implement this in TypeScript? Here is my code in main.ts: content = ["renewal", "payments"] If I use a ...

Why does jQuery only execute the very first condition or none at all if the first condition is false?

I'm trying to create a fixed button that, when clicked, scrolls to a specific section on the page. However, only the first condition seems to be working, and the other conditions are being ignored even when the first one is false. Can you help me figu ...

Clicking on a radio button can trigger the selection of another radio button

I'm currently working on a form that includes 8 radio buttons - 4 for System A and 4 for System B. The options for the buttons are CF, ISO, ISO-B, and NW. My goal is to create a functionality where selecting a radio button in System A automatically se ...

Utilizing lodash or underscore libraries, we can compare two objects and eliminate any duplicate entries

Looking at the image below, I have some json data that consists of three objects; each containing a client's id => data. exact_match : {104} match_4 : {104, 103} match_2 : {104, 103, 68} Is there a way to remove duplicate objects based on pre ...

Pair of dimensions painting with d3 version 4

I am having trouble converting my code from d3 v3 to d3 v4 Below is the original code snippet: var brush = d3.svg.brush() .x(x) .y(y) .on("brushstart", brushstart) .on("brush", brushmove) .on("brushend", brushend); However ...

I can't understand why this question continues to linger, I just want to remove it

Is there a valid reason for this question to persist? I'm considering removing it. ...

What is the reason my Angular attribute is only updated when triggered by a postMessage() method from my iframe?

I am working on a project where I have an attribute named counter that needs to increment by 1 every time a button within an iframe is clicked. To see the code, you can visit: https://stackblitz.com/edit/angular-fznrnf?file=src/app/app.component.ts The c ...

Dynamically incorporating Angular UI Datepicker into your project

For my project, I am required to include a dynamic number of datepickers on the page. I attempted to accomplish this using the following method (Plunker): Script: var app = angular.module('plunker', ['ui.bootstrap']); app.controll ...

Utilize jQuery to extract the content, rearrange the content, and then encapsulate it within fresh

I attempted all day to rearrange this menu the way I want, but since I am new to jQuery, I'm facing some challenges. Here is what I am currently trying to achieve: Inside the last td.top of this menu, there are 3 ul.sub li items. I aim to extract ...

Issues arise when Typescript fails to convert an image URL into a base64 encoded string

My current challenge involves converting an image to base 64 format using its URL. This is the method I am currently using: convertToBase64(img) { var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.he ...

Form showing appreciation message without submission

As a newcomer to Javascript and Jquery, I am working on a form submission feature that should display a "Thank You!" message below the form upon successful submission. While I have managed to submit the form data to our database and show the thank you mess ...