Angular, underscore, extracted list, arrangement

How can I use underscore or angular to sort the unique product grades and product last delivered dates before putting them into a dropdown filter?

product_grades = ["", "40", "20", "30", "35", "45", "50", "60"]

product_last_delivered = ["2015-07-29T00:00:00.000+08:00", "2015-04-10T00:00:00.000+08:00", "2015-11-19T00:00:00.000+08:00", "2015-01-05T00:00:00.000+08:00", "2015-11-18T00:00:00.000+08:00", "2015-11-04T00:00:00.000+08:00", "2015-04-01T00:00:00.000+08:00", "2014-10-15T00:00:00.000+08:00", "2015-10-24T00:00:00.000+08:00", "1899-12-31T23:36:42.000+07:36", "2015-10-20T00:00:00.000+08:00", "2015-06-17T00:00:00.000+08:00", "2015-11-14T00:00:00.000+08:00", "2015-03-12T00:00:00.000+08:00", "2015-07-18T00:00:00.000+08:00", "2015-07-27T00:00:00.000+08:00", "2015-09-21T00:00:00.000+08:00", "2015-10-07T00:00:00.000+08:00"]

Answer №1

Learn about two methods for achieving this goal. The first method involves utilizing the built-in sort function, while the second one employs underscore library.

Useful Functions:

function descending(a, b) {return b - a;}
function str2date(s) {return new Date(s);}
function isNumeric(n) {return !isNaN(parseFloat(n)) && isFinite(n);}

Utilizing Array.prototype.sort Method:

// To sort the grades in descending order after removing non-numeric values.
product_grades.filter(isNumeric).sort(descending);

// To sort dates after converting them from strings to date objects.
product_last_delivered.map(str2date).sort(descending)

Using Underscore's _.sortBy:

_.sortBy(product_grades.filter(isNumeric), descending)
_.sortBy(product_last_delivered.map(str2date), descending)

Recognition:

isNumeric was referenced from this insightful response by DemoUser.

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

Angular: Issue with FormArrays not iterating properly

Apologies for the lengthy post, but I needed to provide a detailed explanation of the issue I am facing. In my form, there is a control that contains an array of JSON data. I have created a reactive form based on this structure. Below are the JSON data an ...

Function returning undefined when accessing prototype property in JavaScript

When attempting to create an image rotator using prototypal inheritance, I am encountering an error in the console showing: TypeError: this.curPhoto is undefined this.curPhoto.removeClass('previous'); I have placed this code in the callb ...

react-ga4 is sending multiple view events repeatedly

After setting up a Google Analytics account and creating a new property, I integrated the tracking ID with react-ga4 for my Album ItemPage as shown below: const ItemPage = () => { const {user} = useContext(AuthContext); let { item } = useParams ...

What is the process for utilizing jQuery's advanced ticker feature to extract content from a text file?

I am currently implementing this code on my website: <script> var file = "http://newsxpressmedia.com/files/theme/test.txt"; function getData(){ $.get(file,function(txt){ var lines = txt.responseText.split("\n"); for (var i = ...

What function does listener() serve within the Redux store?

I'm currently studying Dan Abramov's Redux tutorials on Egghead. At the part where he constructs the Redux Store from scratch, there is this code snippet (around 1:28 - ): const dispatch = (action) => { state = reducer(state, action); lis ...

Convert HTML special characters to ASCII characters using JavaScript

Does Javascript have a specific function for this type of replacement? (replaceAll) PARAMS+= "&Ueberschrift=" + ueberschrift.replaceAll(">",">").replaceAll("<","<"); PARAMS+= "&TextBaustein=" + textBaustein.replaceAll(">",">"). ...

What is the most effective method for implementing COPY/INSERT functionality with cascading effects in PostgreSQL?

Seeking advice on the most effective method to perform an "on cascade copy/insert" of linked elements within PostgreSQL. To better explain my scenario, I've crafted a straightforward example: Understanding the Database Structure Within the datab ...

Manipulate JSON data structure with JavaScript

The initial JSON data: { "data": { "count_at_hub": [ { "hub": "A", "date": "", "size": "1", "count": 141 }, { "hub": "A", "date": "", "size": " ...

Tips for overcoming a script error within the body of a Next.js project

I encountered an error in my _document.js file when trying to add a script to the body of my document. Here is the specific error message that was returned: https://i.stack.imgur.com/QG5zb.png ./pages/_document.js Error: x Expected '}', got &a ...

Transferring information from the backend (powered by nodejs) to the frontend using JavaScript

I have data stored in my MongoDB database and I am retrieving this data from the DB to save it to an array. When a button is clicked on the webpage, I want to send this data to my JavaScript file and use the DOM to display it on the page. On page load, I ...

Tips on merging HTML node lists from various `getElement`s

Is there a way to combine HTML elements and extract values using array methods? I am struggling to merge them as a nodeList. I tried using get elements and array.unshift to consolidate elements into one variable. var elemsLabel = document.querySelecto ...

Tips for verifying the status of an input element following the dynamic addition of a required element

I have implemented a required field for all input elements in a specific state using the following code: for (var i = 0; i < data.length; i++) { // activate required function v = data[i].Required; if (v) document.ge ...

Having trouble with the three.js OBJ MTL Loader in low light situations

In the scenario of "webgl_loader_obj_mtl", it can be observed that the ambient light is not utilized. Even when it is commented out, there is no noticeable impact as the directional light takes precedence. Disabling the directional light results in nothing ...

Building a MultiLevel table in angularjs: A step-by-step guide

I am looking to create a table format that does not use ul li tags, structured as shown below: ----------------- # | name | ----------------- 1 | Level_1-a | 1 | Level_2.1 | 2 | Level_2.2 | 3 | Level_2.3 | 1 | Level_2.3.1| 2 | Leve ...

Experiencing unexpected outcomes via AJAX requests

Linked to: Query Database with Javascript and PHP This inquiry is connected to my previous question. I made adjustments to the PHP script based on one of the responses I received; however, when attempting to utilize $.getJSON, I encountered difficulties. ...

JavaScript nested filtering functions

In my code, I have an array named restaurant_items. This array consists of objects that represent various items from a restaurant. Each object contains another array called in_menu, which also consists of objects. I am looking to filter out the restaurant ...

Step-by-step guide on sorting WordPress posts by a custom field date

I've created an event sidebar section that will only show the next 3 upcoming events. I have successfully set up the custom post type and custom fields, but I am struggling to figure out how to order the posts based on the start date of the events, wh ...

Upload files via Ajax request is required

I am in the process of trying to upload a binary file to a server while avoiding a full page refresh when the server responds. I must admit, I am not well-versed in this area and I understand if my approach needs some adjustments. This is how I have appro ...

Encountering a problem with TypeScript while employing Promise.allSettled

My current code snippet: const neuroResponses = await Promise.allSettled(neuroRequests); const ret = neuroResponses.filter(response => response?.value?.data?.result[0]?.generated_text?.length > 0).map(({ value }) => value.data.result[0]?.genera ...

The process in mocha does not execute when using await

"It" in the Mocha testing framework does not execute when using the await keyword. My approach involves employing functions for asynchronous file reading and then processing the test based on the returned value. I utilize these file read functions multipl ...