Organize intricate JavaScript Arrays

Similar Question:
How to sort an array of javascript objects?

I'm in the process of transitioning some of my older ActionScript 2.0 code to JavaScript. While most things are running smoothly, I've hit a roadblock when trying to numerically sort my array based on score.

In my original ActionScript code, I used:

// Sort the capitals array so that the capitals with the lowest score will be at the top
capitalsList.sortOn("score", Array.NUMERIC);

This method worked fine in ActionScript but doesn't seem to have the same effect in JavaScript. I tried changing it to:

capitalsList.sort("score", Array.NUMERIC);

However, even after this change, "Alabama" is still displayed first (when "Connecticut" should actually appear first since its score is -1). What am I overlooking here?

You can view my script on JS Bin: http://jsbin.com/ibitez/1/edit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
window.onload = function() {

    function capitalsConstructor(capitalName, stateName, score) {
        this.capitalName = capitalName;
        this.stateName = stateName;
        this.score = score;
    }

    // Create state capitals array
    var capitalsList = new Array();

    // All 50 capitals are loaded into the array with a score of 0
    // Sample limited to 10 capitals
    capitalsList[0] = new capitalsConstructor("Montgomery", "Alabama", 0);
    capitalsList[1] = new capitalsConstructor("Juneau", "Alaska", 0);
    capitalsList[2] = new capitalsConstructor("Phoenix", "Arizona", 0);
    capitalsList[3] = new capitalsConstructor("Little Rock", "Arkansas", 0);
    capitalsList[4] = new capitalsConstructor("Sacramento", "California", 0);
    capitalsList[5] = new capitalsConstructor("Denver", "Colorado", 0);
    capitalsList[6] = new capitalsConstructor("Hartford", "Connecticut", -1);
    capitalsList[7] = new capitalsConstructor("Dover", "Delaware", 0);
    capitalsList[8] = new capitalsConstructor("Tallahassee", "Florida", 0);
    capitalsList[9] = new capitalsConstructor("Atlanta", "Georgia", 0);

    capitalsList.sort("score", Array.NUMERIC);

    solution = capitalsList[0].stateName;

    document.getElementById("divSolution").innerText = solution;
}
</script>
</head>

<body>
<div id="divSolution"></div>
</body>
</html>

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

jQuery script unable to alter img src in Firefox and IE browsers

I am working with an image and a jQuery script to change the captcha image. <img src="captcha.jpg" id="my-image"/> The jQuery script is as follows: <script> $(document).ready($("#my-image").click(function(){ $('#my-image&ap ...

Transform the characters within a string into corresponding numerical values, calculate the total sum, and finally display both the sum and the original string

I'm looking to convert a string containing a name into numerical values for each character, ultimately finding the sum of all characters' numerical values. Currently, only the first character's value is being summed using .charAt(). To achie ...

"Why is the comparison function of bcryptjs returning null even though the hash being used is the one that was generated

For security purposes, I securely store hashed passwords in MongoDB. However, when I attempt to compare the stored password with a user-entered string, bcryptjs returns a null value. https://i.stack.imgur.com/4sTXM.png The hashed password is stored in bin ...

Utilizing Object-Oriented Programming to organize and store data within a class object

After successfully running a class to fetch all data and store it in an object, I noticed that the object (AllOptions) is undefined. I have attempted to find a suitable solution but have been unsuccessful. router.get('/:category', (req, res) =& ...

The component 'AddPlaceModal' could not be located in the path '~/components/AddPlaceModal.vue'

Recently, I started incorporating Nuxt for Vue into my projects. In an attempt to enhance my page, I added a new component within the /components folder. However, I encountered a warning during compilation: "export 'AddPlaceModal' was not found ...

What steps should I take to troubleshoot and resolve the connection issue that arises while trying to execute npm install

Following the guidelines from: https://www.electronjs.org/docs/tutorial/first-app I executed commands like mkdir, cd, and npm init. They all ran successfully, generating a file named package.json. Subsequently, I entered npm install --save-dev electron w ...

Utilizing a TypeScript function to trigger an action from within a Chart.js option callback

Currently, I am utilizing a wrapper for Chart.js that enables an animation callback to signify when the chart has finished drawing. The chart options in my code are set up like this: public chartOptions: any = { animation: { duration: 2000, ...

What is the best way to replicate the content of the textarea exactly as it is (with all line breaks and special characters)?

Below is the Laravel form I have, and I need to extract the text in a way that retains its original format: <style type="text/css" media="screen"> #editor { position: absolute; top: 150px; right: 150px; bottom: 15 ...

When using NodeJS with expressJS, remember that req.body can only retrieve one variable at a

I'm having trouble with my login and signup pages. The login page is working correctly, but the signup page is only transferring the password for some reason. App.JS: var express = require("express"); var app = express(); var bodyParser = require("bo ...

Utilize the map function to extract retrieved information

I am currently working on fetching data from the newsapi website. The data is returned in an array-like object format. My main objective now is to iterate through this data and display it using my NewsItem component. Below is the code snippet: export clas ...

I keep receiving the same error (ENOENT) for all npm commands

Currently, I am running windows 8.1 x64 with all the latest updates installed. I encountered an error while using nodejs version 8.9.1 when running the command "npm -v". As a result, I decided to uninstall this version and switch to version 8.9.3. However ...

Fixing the Jquery animation glitch triggered by mouseover and mouseout

In my project, I have implemented a small mouseover and mouseout functionality. The challenge I am facing is that I need to keep the mouseout function using animate() instead of css() for specific reasons. The issue arises when I quickly do a mouseover fo ...

What could be causing the sudden disappearance of the button?

There is an element with the ID "alpha" that contains the text "Now I'm here...". When the button is clicked, only the text should be removed, not the button itself. <div id="alpha">Now I'm here...</div> <button type="button" oncl ...

Ways to invoke a function within a React Material-UI component

Currently, I am in the process of setting up a chat system that allows users to add emojis. To achieve this feature, I have devised a function that produces a component containing both text and an image. Here is the function I have implemented: test: fu ...

Tips for utilizing the "this" keyword in JavaScript in conjunction with the change function

I'm currently facing an issue with the change function. I have two dropdowns that are dependent on each other. Whenever the first dropdown changes, it triggers a change in the second dropdown as well. I achieved this functionality using jQuery AJAX. H ...

The login process in Next-auth is currently halted on the /api/auth/providers endpoint when attempting to log in with the

My Next-auth logIn() function appears to be stuck endlessly on /api/auth/providers, as shown in this image. It seems that the async authorize(credentials) part is not being executed at all, as none of the console.log statements are working. /pages/api/au ...

Generate a spreadsheet file in xlsx format by using the exceljs library in Node

I am currently working with exceljs 3.8 in an attempt to generate a new XLSX file, but unfortunately the code below seems to be malfunctioning. createNewExcelFile: function (excelFilePath) { //excelFilePath: Path and filename for the Exce ...

Tips for determining whether the current request is an AJAX request in MVC3 view using JavaScript

I have a div with a maximum height and overflow set to auto. When the size of the div overflows, a scroll bar is automatically added. However, I have also set $("#itemsdiv").scrollTop(10000); so that the scroll bar is always at the bottom. Now, I want t ...

Generating dynamic dropdown menus using data from a database with the help of PHP and Ajax technologies

I'm currently working on creating a dynamic dropdown menu that will be populated with data retrieved from a database. I've hit a roadblock in parsing the data from a multidimensional array sent by a PHP file. Here's a snippet of my code: Se ...

How to Use AJAX to Read a Text File in JavaScript

Having trouble with AJAX! I have successfully set up a marquee on my website, but now I want it to dynamically fetch the text from a text file. The goal is to read the text from the file (which contains only one line) and assign it to a global variable nam ...