Determine the number of distinct property values within a JSON API response

Running on a Ruby on Rails backend, I have a JSON API that serves an array of objects with the following structure:

{
  "title_slug": "16-gaijin-games-bittrip-beat-linux-tar-gz",
  "platform": "Linux",
  "format": ".tar.gz",
  "title": "BIT.TRIP BEAT",
  "bundle": "Humble Bundle for Android 3",
  "unique_games": 9
},
{
  "title_slug": "17-gaijin-games-bittrip-beat-linux-deb",
  "platform": "Linux",
  "format": ".deb",
  "title": "BIT.TRIP BEAT",
  "bundle": "Humble Bundle for Android 3",
  "unique_games": 9
},

The challenge lies in counting only unique titles, as there are different types of downloads for each title within the object array. My initial idea of implementing this in the Ruby on Rails model failed because it required processing the entire array.

Considering I use AngularJS on the frontend and have to update the number of unique titles displayed on a table, I believe filtering should be done in the controller.

To provide more context, here's a screenshot of the page where these functionalities will be integrated: https://i.sstatic.net/DFx3L.png

Thank you,

Thomas Le

P.S. This project is not meant for public access; it's a database site housing information from bundles purchased from IndieGala and HumbleBundle. The functional enhancements are geared towards making the open-source project on GitHub more accessible for personal use.

If you're wondering about the presence of Humble Bundle data in the image shared, now you know why!

Answer №1

http://jsfiddle.net/hy7rasp4/

To efficiently organize your data into an array based on a unique identifier, you can easily track duplicates and their count.

let index,
    category,
    uniqueInfo= {};

for (index in info) {
   category = info[index].category;
   if (!uniqueInfo[category]) {
      uniqueInfo[category] = [];
   }

   uniqueInfo[category].push(info[index]);
}

Answer №2

Perhaps consider restructuring your data simultaneously to facilitate easy access to items later and quickly determine the number of titles, for example in JavaScript

// assuming arrayOfObjects

var objectOfTitles = {},
    i;
for (i = 0; i < arrayOfObjects.length; ++i) {
    if (!objectOfTitles.hasOwnProperty(arrayOfObjects[i].title)) {
        objectOfTitles[arrayOfObjects[i].title] = [];
    }
    objectOfTitles[arrayOfObjects[i].title].push(arrayOfObjects[i]);
}

var numberOfTitles = Object.keys(objectOfTitles).length;
// then say you choose a title you want, and you can do
// objectOfTitles[chosenTitle] to get entries with just that title

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

I am looking to showcase images beside individuals' names or photos on my website in a vertical arrangement, similar to how it is done on Facebook

Looking for suggestions on how to display images uploaded by users on my webpage in a way similar to Facebook, with the user's photo displayed beside each image. Any recommendations or website links would be greatly appreciated. Thanks, Santosh Sahu ...

Managing Nested Elements in State in ReactJS

Check out the code snippet below: import React,{useState} from 'react' const iState ={ Name : '', Email :'', Salary :0, Error:{ EName:'*', EEmail:'*', ESalary:'* ...

Alter the button's color seamlessly while staying on the same page

I have implemented a feature that allows me to change the category of photos without having to leave the page and it works perfectly. My next goal is to create a button system where the pre-defined category of a photo is indicated by a button with a green ...

Are DOM Events compatible with ajax-loaded content that cannot be unloaded?

How should events be managed with ajax-loaded content that can be hidden or displayed? I have created a sidebar in HTML that toggles visibility on click, along with two pages that are loaded using ajax. When the sidebar is hidden or displayed, I need to ...

Error: Unable to call dispatch method on this.$store object

I'm diving into Vue and hitting a wall with this error message. TypeError: this.$store.dipatch is not a function I've set up the store.js file and am attempting to call actions from within a vue file. I've scoured the internet for answers ...

A guide on utilizing Retrofit and GSON to effectively parse data from the WordPress REST API

I'm facing an issue with parsing a list of data from this JSON structure! Here is the JSON data I am working with: [ { "id": 17502, "link": "https://www.angrybirds.com/blog/get-ready-angry-birds-movie-2-premiere-new-game-events/" ...

Clicking React useState causes it to update before the intended click is registered

My goal is to create 50 buttons that, when clicked, will update the value of useState to the current button number (array index+1). However, I've encountered an issue where I have to click a button twice to get the correct value. The first click alway ...

Encountered an issue while attempting to write to SQLite, error code 6 is

I'm working on a project that involves writing to a SQLite Database from a cross-platform app built with AngularJS, Monaca, and Onsen UI. Within my app, there's a view where users input their username and password. I store these details in a Ser ...

Tips on restricting users to choose dates that are later than the current date

Currently, I am working with Vue3 using the options API. After reviewing this StackBlitz, my question is regarding how to correctly set the :max value for a date-picker. Even though I have assigned :max as new Date(), I am still able to select dates that ...

Can all anchor tags properties on a website be simultaneously changed?

Recently, I discovered that using target="_blank" in anchor tags can leave a website vulnerable to security risks, and the recommended alternative is to use rel="noopener". In my current web project, all anchor tags are currently utilizing the target attri ...

What is the process of invoking the POST method in express js?

I've been diving into REST API and recently set up a POST method, but I can't seem to get it to work properly. The GET method is running smoothly in Postman, but the POST method is failing. Can someone lend a hand in figuring out where I'm g ...

What is the method to invoke a function within a factory in angularjs by using a string parameter?

I have a complex logic that I want to encapsulate in an AngularJS factory for easy use with dependency injection. The challenge is that the logic is dynamic, so I don't know in advance what functions will be available. What I have is a string represen ...

"Learn how to clear an input field using jQuery with this simple guide

I've gone through a few discussions, such as this one and that one, but I'm still struggling to clear the input field after submission. What is the simplest way to achieve this? This is my current approach: $(document).ready(function(){ $(& ...

Add a hovering effect to images by applying the absolute positioning property

On my React website, I am utilizing Tailwind CSS and I am attempting to showcase an image that has interactive elements. I want certain parts of the image to increase in size slightly when hovered over. My approach to achieving this was to save each part o ...

Create shorter nicknames for lengthy reference names within the ng-repeat loop

Is it possible to assign an alias to a long reference name in ng-repeat? Currently, I have 2 complex objects where one acts as a grouped index for the other. Although the ng-repeat template code is functioning correctly, it's getting hard to read and ...

Tips for refreshing the Vuex store to accommodate a new message within a messageSet

I've been working on integrating vue-socket.io into a chat application. I managed to set up the socket for creating rooms, but now I'm facing the challenge of displaying messages between chats and updating the Vuex store to show messages as I swi ...

Arrange a JavaScript map based on its values and ensure that a specific field index remains at the top position

I'm sure this question may seem simple to some, but as a JavaScript novice, I couldn't find the answer myself. Here is the code snippet I'm working with: Let map = new Map<String,String> map.set('0', select) map.set('1&a ...

"HTML glitches in Onsen 2: A Dynamic Dilemma

During runtime, my app context is dynamically generated. In Onsen 1.3, I utilized the following function to create and compile the HTML: $scope.generateFrom = function (div) { var el = div; var html = HTMLGenerator.getHTML(); el.in ...

What is the best way to ensure jQuery loads before CSS code on a website

I am currently working on a blog project that allows visitors to customize the background color by simply clicking on one of five available buttons. $(function(){ //Checking if a color scheme has already been selected var chosenColor = ...

Variety of part ingredients

In my component, I have a button and include another component which also contains a button. How can I align these two buttons next to each other without using absolute positioning? When I try positioning them using absolute right and top values, the lay ...