What is the best way to organize variable values in conjunction with their corresponding variable names?

Among the 7 variables - rodeo, saya, balthazar, mistral, luna, calypso, and kiara - each holds a value obtained from calculations in the initial part of my program.

My goal is to arrange these variables in ascending order based on the values they contain, while still being able to differentiate them by name.

I am currently in the process of creating a website for my 11-year-old daughter using Wix.

var points = [rodeo, mistral, calypso, kiara, balthazar, saya, luna];
points.sort(function(a, b){return a-b});   
console.log(points)   

Running this code gives me:
0: 2
1: 2
2: 2
3: 2
4: 3
5: 3
6: 4

Although the ranking is correct, I am unable to determine which value corresponds to each variable, which is the issue I am currently facing.

Answer №1

To achieve this, it is recommended to utilize an array of Object. You can utilize the find() method to retrieve the value associated with a specific name.

var points = [{name:"rodeo",value:3}, {name:"mistral",value:3},{name:"calypso",value:1}, {name:"kiara",value:6}, {name:"balthazar",value:3}, {name:"saya",value:2}, {name:"luna",value:4}];
points = points.sort((a,b) => a.value - b.value);
console.log(points)
console.log("value of kiara = " + points.find(({name}) => name === "kiara").value)

Answer №2

If you want to organize your variables within an array and then sort them based on their values, one approach could be to store your variables as objects within the array. This way, you can easily associate each "variable" with its corresponding value.

Take a look at the example below:

var rodeo = 3;
var mistral = 2;
var calypso = 2;
var kiara = 2;
var balthazar = 2;
var saya = 4;
var luna = 3;
var points = [{rodeo}, {mistral}, {calypso}, {kiara}, {balthazar}, {saya}, {luna}],
result = points.sort(function(a, b){return Object.values(a)[0]-Object.values(b)[0]});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Attempting to decode the string prior to selecting an item from the Vue.js/Nuxt array

Hey there, I really appreciate anyone who can assist me with this. I've been dabbling in Laravel for a few months and now I'm trying to dive into studying Nuxt. The specific type of translation I need help with is proving to be quite challenging ...

React - Ensure useEffect is triggered only after state update

One of my components (ItemsIndex) is using a custom hook to fetch data from our API. However, the ItemsIndex's useEffect runs first, causing the DOM to not be filled with elements that could be scrolled into view. How can I make sure that the useItems ...

Using the scroll feature in the selectyze plugin allows for smooth

The jQuery plugin selectyze from https://github.com/alpixel/Selectyze serves to replace the standard selectbox (dropdown), but there is a small issue that can be quite irritating. I am hoping someone out there may have a solution. Annoying Situation Here ...

Error: Element type is invalid: a string was anticipated, but not found

I recently experimented with the example provided in React's documentation at this link and it functioned perfectly. My objective now is to create a button using material-ui. Can you identify what is causing the issue in this code? import * as R ...

Neglecting to automatically align text

My goal is to automatically align text based on the language, so that Arabic text starts from the right and English text starts from the left. After some online research, I discovered that I need to use dir="auto" in the tag and text-align: auto; in the CS ...

Creating a Wireframe in Three.js Using a RawShaderMaterial with LineSegments

In the midst of my work on a project, I encountered a dilemma with rendering an intricate wireframe using THREE.LineSegments. My objective is to gradually animate the vertices within this LineSegments material (referred to as warehouse in the project), but ...

Packages required for plugins on Npm

I am fairly new to NPM dependencies and I'm currently transitioning from the Ruby world. Working on a Chrome extension utilizing React, in my content.js script, I have the following code at the top: var React = require("react"); var $ = require("jqu ...

Invoke multiple stored functions using JavaScript, beginning with this one

One way to store and call a function later is by assigning it to a variable, like so: var storedFunction = functionName; //Later On storedFunction(); //Equivalent to functionName() If you want to store a function and also execute 'this' when ca ...

Employing double quotes within JSON keys or values can help enhance the readability and

Is there a way to include double quotes within my JSON data? I've been struggling with this issue. Below is the example of my JSON: "I": { "1: Vehicle Control:M": { "D2": { "VM": "3300.00", "VSD": ...

Attempting to nest an AJAX request within the success callback of another AJAX request

I am attempting to execute two jQuery ajax calls, with the second call being made from the success callback of the first. I have experimented with different variations of the code, such as adjusting the brackets. Below is my attempted code snippet: $.aja ...

Sort data by checking either multiple or single checkbox options in AngularJS; if none are selected, display all results

My goal is to implement a filter in my AngularJS code that will filter data based on selected checkboxes. If no checkbox is selected, all results should be displayed without any filtering. Currently, the issue I am facing is that the data is only displayed ...

Why are users not visible in the Node.js application?

Good day! I am a newcomer to this community and I'm hoping to get some guidance in the right direction with my inquiries. Recently, I came across some code on Github that I am trying to improve upon. The code can be found at https://github.com/voroni ...

Take the inputs, calculate the total by multiplying with the price, and then show

I'm in the process of developing a simple PHP form for an online t-shirt order system. Most aspects are running smoothly, but I'm facing issues with the final calculations. My goal is to combine the quantities based on sizes, multiply them by the ...

Tips for customizing the Bootstrap 4 grid system for a seamless mobile viewing experience

My form consists of 3 steps, each row containing multiple inputs. You can see an example of my form here. Below is a sample of the grid system I have used: <div class="row"> <div class="col"> <input asp-for="First_N ...

Looking for assistance with implementing the Vue CDN script in a Laravel Blade template

Hey everyone, currently I am working on a project called Register Page. My goal is to incorporate Vue cdn and Vue Consoles within the blade template. I have implemented a show/hide option without any console errors appearing. However, when I click on the ...

"Executing a query on Angular Firestore using the where clause fetches all documents from the

I am encountering a perplexing issue with my angular app that is connected to Firestore. Despite following the documentation closely, when I query for documents in a collection based on a specific condition, the array returned contains every single documen ...

Console displaying message of comfort twice - ReactJS

I have a simple app that increments the count from 10 to 11 in the componentDidMount life cycle, but for some reason, the numbers 10 and 11 are appearing twice in the console. I would like to understand why this is happening. Here is the code snippet: im ...

Data cannot be transferred to a child element unless it has been initialized during the definition phase

Passing an array data from parent to child component has brought up some interesting scenarios: parent.component.html: <child-component ... [options]="students" > </child-component> Status I: Setting the array on definition ...

Does the presence of XHR calls with application/json as the 'type' indicate that the application is using AJAX technology?

Forgive my lack of knowledge on this topic as I am still in the process of learning. I am exploring the possibility of implementing a solution on my website where I can embed a JavaScript code that will make API calls to display images. I am curious to kno ...

Adding a class to an element in AngularJS

I have a question about entering empty values in an input field and highlighting its boundary. I added a new class 'warning' for this purpose and created the following code. HTML: `<body ng-app="TestPage"> <form ng-controller="TestFor ...