Navigating through items and organizing based on several criteria

In JavaScript, I am currently learning about accessing objects and sorting them based on multiple conditions. As a beginner in JavaScript, this may seem like a very basic question to some. My task involves sorting based on the 'status' field.

var issues = cf.getEventMessage(reply); // All data stored in 'issues'
var assigneeTasks = {};
for(var i=0; i<issues.length; i++){
    var record = issues[i];
    if(assigneeTasks[record.assigneemail] == undefined){
        assigneeTasks[record.assigneemail] = [];
    }
    assigneeTasks[record.assigneemail].push(record); // Sorted according to 'assigneemail'
}

Now the variable assigneeTasks contains:

{"[email protected]":
[
{"id":"T-728","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Open"},
{"id":"T-727","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-726","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-10-04","status":"Open"},
{"id":"T-679","assignedTo":"devt","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Under Review"},
{"id":"T-645","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-09-27","status":"In Progress"}
],

"[email protected]":
[
{"id":"T-728","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Open"},
{"id":"T-727","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-726","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-10-04","status":"Open"},
{"id":"T-679","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Under Review"},
{"id":"T-645","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-27","status":"In Progress"}
]
}

The desired output should be:

{"[email protected]":
[
{"id":"T-728","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Open"},
{"id":"T-726","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-10-04","status":"Open"},
{"id":"T-727","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-645","assignedTo":"dev","assigneemail":"[email protected]","duedate":"2017-09-27","status":"In Progress"},
{"id":"T-679","assignedTo":"devt","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Under Review"}
],

"[email protected]":
[
{"id":"T-728","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Open"},
{"id":"T-726","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-10-04","status":"Open"},
{"id":"T-727","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-645","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-27","status":"In Progress"},
{"id":"T-679","assignedTo":"paul","assigneemail":"[email protected]","duedate":"2017-09-29","status":"Under Review"}
]
}

Answer №1

If you're looking to arrange an array of object values, try out the following code snippet for sorting based on the number in the ID:

function extractNumber(string) {
  return parseInt(id.replace(/[^0-9]+/g, ''));
}

function compareValues(a, b) {
  a = extractNumber(a.id);
  b = extractNumber(b.id);
  if (a < b) {
    return -1;
  }
  if (a > b) {
    return 1;
  }
  return 0;
}


Object.keys(obj).forEach(function(key) {
   output[key].sort(compareValues);
});

To incorporate sorting by date as well, refer to the solution provided in this Stack Overflow post: Sort Javascript Object Array By Date

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

Cookies in Node.js Express are not being incorporated

Currently, I am in the process of developing a nodejs application and facing an issue with defining cookies. Here is a snippet of the code that I am working with: var app = express(); app.set('port', process.env.PORT || 3000); app.set('vie ...

Is there a way to eliminate get variables and filename from a URL using JavaScript or jQuery?

I've been researching this issue, but unfortunately, I haven't been able to find a definitive solution for my specific needs. Let's say I have a URL like... How can I extract this URL and remove the "index.php?search=my+search" part so that ...

How to transfer data from an HTML form to PHP using AJAX

I've encountered an issue while working on a simple application that consists of one HTML file communicating with a PHP page using AJAX. Take a look at my index.html below: <!DOCTYPE html> <html><head> <meta charset="utf-8"> & ...

What is the best way to utilize useEffect solely when the length of an array has

Is there a way to trigger a state update only when the length of the prop "columns" changes? useEffect(() => { if (columns.length !== prevColumns.length) { // update state } }, [columns]); Any suggestions on how to achieve this? ...

Choose the initial selection from a dropdown menu using AngularJS

I am facing a minor issue where I want the first element in my select dropdown to be displayed properly rather than just a blank space. Below is the code snippet I am currently working with: <select style="border-radius: 4px;" class="form-control" ng- ...

Calculate the total amount by multiplying the price and quantity values within nested arrays

I have a collection of objects with nested arrays structured like this [ { orderId: 123, orderStatus: 'Pending', date: 'June 13, 2020', products: [ {product: 'choco', price: 300, qty: 3}, {product: 'm ...

Interactive chat feature with live updates utilizing jQuery's $.Ajax feature for desktop users

I came across a script for real-time chat using $.ajax jQuery, but it only refreshes my messages. Here's an example scenario: I type: Hello to You, and I see this message refreshed. You reply: Hey, in order to see your message, I have to manually refr ...

Customizing the JavaScript code created by the "Change Variable by..." block in Blockly

I'm currently working on a Blockly Project where I am passing data from the blocks as JavaScript code. One of my goals is to make some adjustments to the output code in order to make it more user-friendly for beginners. While it is possible to modify ...

Matching Tables with JavaScript and JSON

After hours of coding, I'm stuck on a simple task and could really use some assistance. The "users" object contains user account information, with the function "get" meant to retrieve matching objects from this array. var users = [ { name ...

I am struggling to figure out the best way to save JSON data retrieved from an API request in a Node.js Express server, and then efficiently send it to a React Native client

My Node.js server is up and running with an app.js file structured like this: var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-pars ...

I have created a textbox with a button, but I am unsure of how to delete it

var emailFields = document.getElementById('emails'), addLinkButton = document.createElement('a'), fieldTemplate = emailFields.getElementsByTagName('div'), currentFieldCount = fieldTemplate.length, maxFields ...

Docusaurus font loading specifically optimized for body text, excluding headings

I've added the following CSS code to my Docusaurus custom stylesheet: @import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700&display=swap"); :root { --ifm-color- ...

Stopping JavaScript when scrolling to the top and running it only when not at the top

I found a great jQuery plugin for rotating quotes: http://tympanus.net/codrops/2013/03/29/quotes-rotator/ Check out this JSFiddle example: http://jsfiddle.net/LmuR7/ Here are my custom settings (with additional options that I haven't figured out yet) ...

What is the method for determining the numerical worth of the px containers?

https://i.stack.imgur.com/0K2TD.png Total width of the bar is set to 500px, with the red box at a width of 150px, the yellow box at 200px, and the green box at 50px. CSS Styles: .box { float:left; width:150px; box-shadow:3px 3p ...

Result of a callback function

Having trouble returning a value for form validation using a callback function. It's not working for me... <form action="loggedin.php" onsubmit="return test(valid)" method="post"> function test(callback) { var k = ""; var httpRequest = ...

What is the best way to send an HTTP request in AngularJS to receive data in JSON format?

I am trying to create an AngularJS app that can send HTTP requests for JSON data. I have written the code in my index.html file to request JSON data using AngularJS, but for some reason, the JSON data is not being printed. When I check the console in Fire ...

Can HTML text areas be designed to adjust their width automatically, as well as their height?

Among the numerous StackOverflow examples showcasing an auto-height Textarea, one noteworthy example can be found here: <textarea oninput="auto_grow(this)"></textarea> textarea { resize: none; overflow: hidden; min-heig ...

Modify the `div` content based on the selected items from the bootstrap dropdown menu

My navigation bar is built using Bootstrap and contains multiple drop-down items. Now I have another div with the class of col-md-3. Within this div, I would like to display the items of the dropdown menu when hovered over. Currently, hovering over a dro ...

Is there a way in AngularJS utilizing ui-router to retrieve the ngResource error from $stateChangeError when a request fails in the resolve section of a state?

Check out this plunker where I am facing an issue: http://plnkr.co/edit/vdctsTcMq4nD1xpUl3pu The problem is that the $resource promise is being rejected due to a request failure (404 error). I am aware that I can handle the error within the resolve block ...

Querying a collection with a bulk request using Mongoose Cursor

My current challenge involves working with rxJS and handling bulk HTTP requests from a database containing over 1 million documents. The code I have is relatively simple. I am pushing all the documents from the collection into an array called "allPlayers" ...