Tips for creating a group in underscore.js

{
    "Name": "User1",        
    "SlotStartDate": "05/16/2018",
    "SlotStartTime": "9:20AM"        
},
{
    "Name": "User1",   
    "SlotStartDate": "05/16/2018",
    "SlotStartTime": "10:20AM"
},
{
    "Name": "User1",        
    "SlotStartDate": "05/17/2018",
    "SlotStartTime": "9:20AM"        
},
{
    "Name": "User1",        
    "SlotStartDate": "05/17/2018",
    "SlotStartTime": "10:20AM"        
},
{
    "Name": "User2",
    "SlotStartDate": "05/16/2018",
    "SlotStartTime": "8:45AM"
},
{
    "Name": "User2",
    "SlotStartDate": "05/16/2018",
    "SlotStartTime": "9:25AM"
}
{
    "Name": "User2",
    "SlotStartDate": "05/17/2018",
    "SlotStartTime": "8:45AM"
},
{
    "Name": "User2",
    "SlotStartDate": "05/17/2018",
    "SlotStartTime": "9:25AM"
}

Would appreciate assistance on using underscore.js to group the above data and return an array organized by user and date as follows:

Data grouped by user and date.

User1
- "05/16/2018"
    - "9:20AM"
    - "10:20AM"
- "05/17/2018"
    - "9:20AM"
    - "10:20AM"     
User2
- "05/16/2018"
    - "8:45AM"
    - "9:25AM"
- "05/17/2018"
    - "8:45AM"
    - "9:25AM"          

I have attempted the code snippet below:

var createGroups = _.groupBy(response2, function(value){
    return  value.Name + '#' + value.SlotStartDate  
});

var groupedData = _.map(createGroups,function(createGroup){
    return {                            
        slottimes: _.pluck(createGroup,'SlotStartTime')
    }
});

Your support is highly valued!

Answer №1

You have the ability to achieve this task:

var groupedData = _.mapObject(_.groupBy(data, 'Name'), (user) => {
  return _.mapObject(_.groupBy(user, 'SlotStartDate'), (date) => {
    return _.pluck(date, 'SlotStartTime');
  });
});

Here is a link to a jsfiddle example

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

Retrieve the identification number from the code snippet containing the "append(<tr><td="ID">..." template

I have a table that I'm populating with data from a firebase database using the append() function. $("#table_body").append("<tr><td>" + name + "</td>" + "<td>" + brand + "</td>" + ...

Unable to access a JavaScript array in one file from another file in JavaScript

I am having trouble referencing the arrays I created in a.js and then using them in b.js. The arrays are declared inside a.js $(document).ready(function () { categoryarray = []; productarray = []; In my HTML file, I have the following script tags: < ...

Extend and retract within a row of a table

I need help with modifying a dynamically generated table to meet specific requirements. The table currently looks like this. The task involves hiding all columns related to Category B, eliminating duplicate rows for Category A, and displaying entries from ...

Even after setting a new value to a variable in Vue, it may still reference the old

init(){ this.unsortedList = this.selectedVoucher.approvalStepList; // list in original order this.sortedList = this.unsortedList .sort(function(a,b){ if (new Date(a.createDate) < new Date(b.createDate)) return -1; ...

Personalize your jQuery function

Is there a way to enhance jQuery functions like hide() or show()? Specifically, I want my custom function to be executed along with the base jQuery function whenever I use hide(). If this is possible, could you please demonstrate how? Thanks. ...

Is it possible to efficiently delete multiple elements from an array in Android Studio? If so, what

I'm currently working on developing an app and encountered a problem. I need to remove two elements from an array, but Android Studio only allows me to remove one. If I try to remove another element, the app crashes. Below is my code where I have only ...

Improved method for managing hash URLs in AJAX-enabled websites

When dealing with pages that have a hash mark in the URL address bar, it can be inconvenient unless using HTML 5 and history.pushState() for AJAX loads. For example, when item1.html is loaded and then the user clicks to view item2.html, the URL changes to ...

What are the best ways to creatively design HTML content generated using JavaScript?

I am currently using Squarespace 7.1 to create a website that features a randomly generated quote in the footer section. The quote is generated from a JavaScript array when the page loads, reloads, or changes. Despite the code functioning as intended (test ...

Guide on assigning a value to a property within a PHP object

I am looking to update two variables within the Woocommerce cart object. I have inspected the arrays structure within the object using: echo 'Cart Dump: ' . var_dump($woocommerce->session->cart) This output shows: array(1) { ["01822dd92b ...

Switch classes while navigating within a div

My website has a sticky sidebar with a list of cars and their corresponding categories in a table: <ul class = "cars"> <li class=""><a href="javascript:void(0)" class="model" data-id="1"> BMW </a></li> ...... ...

A thrilling twist on the classic game of Tic Tac Toe, where X and

I am having trouble with switching between the cross and circle symbols in my Tic Tac Toe game. The code seems to be set up correctly, but it's not functioning as expected. Any suggestions on how to fix this? Here is the JavaScript code: varcode va ...

Tips for Retrieving the Key Names of JSON Objects within a JSON Array

I'm trying to retrieve the object names "channelA" and "channelB". Here is the JSON data: [ { "channelA": { "programmes": [ { "start_utc": 1522208700, "stop_utc": 152220 ...

Retrieving an image URL from JSON information

I am currently working on a project where I need to fetch data from a PHP file that retrieves information from a database table. The specific result I am looking for is the 'pageLocation' of a single record from the 'page' table. This d ...

How can PHP be used to create Javascript?

Is there a specific library or tool tailor-made for PHP programmers to seamlessly transition their logic into Javascript? For example: $document = new Document($html); $myFoo = $document->getElementById("foo"); $myFoo->value = "Hello World"; Which ...

Having issues with AngularJS rzslider failing to dispatch updated slider value

Hello, I am currently using the rzmodule/rzslider in AngularJS. However, after changing the slider to a specific range, the ng-modal is not returning the updated value. It keeps returning the initial value of 10000 that was configured initially. $scope.sl ...

Applying a consistent script with varying inputs on the same HTML page

Is it possible to create a JavaScript code that can be used across different sections of an HTML document? The goal is for the script to fetch data such as title, runtime, and plot from a specific URL request and insert this information into the appropriat ...

`The best way to access a JavaScript variable from PHP`

This is the code snippet I am working on: var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day; document.form1.bookingsession.focus(); var coords = { "lat": daaa }; $.get('bs.php', coords, function () { ...

Inconsistent display of Bootstrap tooltip upon mouseover

Utilizing an icon from Font Awesome to showcase text in a tooltip upon mouseover with Bootstrap, I encountered an issue where the tooltip only displays when hovering over the left half of the icon. This discrepancy occurs on both Edge and Chrome browsers, ...

The higher-order component is lacking a defined display name in its definition

Struggling to develop a higher order component, I keep receiving an eslint warning. The component definition is missing a display name. Even after attempting to include a display name as shown below, the warning persists. import React from 'react ...

The file from Connect Asset Manager does not correspond to the requested URL

Anytime I try to access a js file (http://localhost:3000/static/js/backbone.js), it keeps displaying the cached file from the asset manager, which in this case is showing jquery.js instead of backbone. Below is the code snippet I am currently using: con ...