Based on the alphabet, the first name precedes the last name

Looking for a way to identify students in an array where the first name comes before the last name alphabetically. Need to organize the list in descending order by full name using Underscore.js. Any assistance is appreciated.

Answer №1

Demo

// Creating an array of students.
var students = [ {
    firstname: 'Michael',
    secondname: 'Jordan'
}, {
    firstname: 'LeBron',
    secondname: 'James'
}, {
    firstname: 'Kobe',
    secondname: 'Bryant'
}, {
    firstname: 'Shaquille',
    secondname: 'O\'Neal'
}, {
    firstname: 'Magic',
    secondname: 'Johnson'
}];

// Filtering students whose firstname comes before secondname.
var testStudents = _.filter(students, function(student){
    return student.firstname < student.secondname;
});

// Sorting the array in ascending order.
testStudents = _.sortBy(testStudents, function(student){
    return student.firstname + ' ' + student.secondname;
});

// Reversing the array.
testStudents = testStudents.reverse();

// Displaying the result.
console.log(testStudents);

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

Is it possible to define multiple regular expressions for a single field using yup in a form validation?

My goal is to validate an input to only accept URLs in Google Maps format. There are three possible valid outputs for a Google Maps URL: (for Google Maps app) (I only want to focus on ) (when sharing directly from Google Maps) Currently, I am able to ...

Can the identical script be implemented by utilizing two separate JavaScript files?

My knowledge of JavaScript is limited, so I often rely on resources like JavaScript.com and Dynamic Drive for coding. Recently, I came across some scripts that reference two different .js files. <script type="text/javascript" src="lib/prototype.js"> ...

serving static content on subdomains with Express JS

I created an API using Node.js Express JS and utilized apidocjs.com to generate the static content. The issue I am currently facing is that I want the documentation file to be located under a subdomain such as docs.example.com or api.example.com/docs. What ...

Enabling and disabling contenteditable functionality on a div element in Angular

Issue I am facing an issue while trying to bind a boolean to the contenteditable directive of a div. However, it seems that this binding is not working as expected. Error Message: The 'contenteditable' property is not recognized as a valid p ...

Is there a way to retrieve text content from a modal using JavaScript?

I am using ajax to fetch values from my table. $(data.mesIzinTanimList).each(function (index, element) { if (element.izinTanimiVarmi == 'yok') tr = "<tr class='bg-warning text-warning-50' row='" + element. ...

Express.js: Communicating with internal microservices

I'm still learning about node.js, express.js and REST APIs. Here's the situation I'm facing: I have a requirement to retrieve user information from my database (using mongoDB) in various scenarios. What would be the recommended approach i ...

A script page in Wordpress generates a numerical value in the URL

I created a script named search.php which utilizes various search engines APIs to display search results. From this file, I have developed a Page template and incorporated the simplePagination plugin The issue arises when I click on a page within the pag ...

"I encountered an error while sorting lists in Vue 3 - the function this.lists.sort is not

Creating a Vue 3 front-end template: <template> <div class="container"> <router-link to="/user/create" class="btn btn-success mt-5 mb-5">Add New</router-link> <table class=" ...

Why are Three.js ply files lacking in color?

I have been experimenting with the Three.js example located in the directory: three.js/examples/webgl_loader_ply.html By replacing their .ply file with my own (created in Blender). In Blender, I used Vertex Paint to paint vertices. Before exporting to . ...

Easily adding multiple field data with the same field name into a database can be achieved using Mongoose and Node.js (specifically Express

I am new to using nodejs and exploring the creation of a project focused on generating invoices. My goal is to store product information in mongodb utilizing mongoose and express, but I'm unsure of how to proceed. Below is a snippet of my HTML code.. ...

What steps should I take to resolve the error message "Error: srcmain.ts is not found in the TypeScript compilation?"

I've exhausted all possible solutions on StackOverflow and have even gone as far as uninstalling both Node and Angular three times in the span of three days. I'm completely stumped as to why this issue keeps occurring specifically when using "ng ...

Is it possible for AngularJS components to alter their enclosing element?

I see Angular components as element directives. Take a component named hero, for example, I can include it in the parent template like so: <hero myparam="something"></hero> What I envision is using the hero element as a container managed by t ...

Is it possible to resize the output image in Three.js post-processing?

My game tends to lose performance when I switch to fullscreen mode, but it runs smoothly when the screen is small. I'm wondering if there's a way to render the game in small screen mode first and then scale up the processed render. I'm goin ...

What are some methods for controlling a webpage? Is it through HTML, JavaScript, Xpath, or

Hey there, I could really use your expertise on untangling a question that has me completely stumped. What exactly is the mechanism for controlling a webpage? a. HTML b. JavaScript c. Xpath d. CSS ...

Exploring the integration of data objects within a style tag in Vue.js

I'm currently experimenting with creating a parallax effect using background-position and a data object. <div class="parallaxBanner" :style="{scrollBanner}"> </div> <script> export default { data: function(){ ...

How can I create a detailed highchart map of Korea that accurately includes Dokdo in its design?

I am currently working on a project to create a comprehensive map of Korea using highchart. One major challenge I have encountered is the absence of Dokdo on the standard map of Korea provided by highchart. Is there a way for me to develop a map that inc ...

Issue with toggleClass not functioning properly after receiving data from an AJAX call

On my website, I have a link that triggers an AJAX request. However, when the response comes back and I try to use the toggleClass function in the following .js code snippet, it doesn't work as expected: $(document).ready(function(){ $("td").click( ...

Unable to find the element using the text "selenium webdriver"

Currently, I am working with Selenium WebDriver using Java. Log.info("Clicking on To weekrange dropdown"); JavascriptExecutor executor25 = (JavascriptExecutor)driver; executor25.executeScript("document.getElementById('toWeekYear).style.display=' ...

Exploring the Integration of Callbacks and Promises in Express.js

I'm currently developing an Express.js application where I am utilizing x-ray as a scraping tool to extract information. For each individual website I scrape, I intend to establish a unique model due to the distinct data and procedures involved in th ...

NodeJS controller function to generate and send a response

I'm facing an issue with my controller method where I am unable to send a response to the user, even though the value is displaying in the console. Below is my code snippet: const hubHome = (req,res) => { const hubId = req.params.hubId; fetch ...