Get the first element of a 2-dimensional array after sorting it

In my possession is the following 2D array:

[["fox", "100"], ["the", "1"], ["quick", "50"]]

The task at hand is to keep only the first element of the array, but have it sorted based on the second value.

The expected result should be: the,quick,fox

A loop has been implemented that iterates over the first elements successfully, however arranging them in order of the second value remains a challenge:

for (var i = 0; i < inputArr.length; i++) {
  x1 += inputArr[i][0];
  if(i != inputArr.length - 1){ 
   x1 = x1 + ",";
  }
 }

  
Write(x1); //outputs -> fox,the,quick

Answer №1

To organize the elements of an Array, you can utilize the sort function. This function requires a lambda function with two parameters to determine the order based on the return value.

let arr = [["fox", "100"], ["the", "1"], ["quick", "50"]];

let output= arr.sort((a,b) => a[1]-b[1]).map(e => e[0]).join(",");

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

Error message "Unable to load / invalid sound with zero-length duration reported on Chrome and Firefox."

Encountered an issue where / invalid sound zero-length duration was reported in Chrome and Firefox. However, the error only appeared in IE10. Here is the code snippet: foo = soundManager.createSound({ id: 'bar', url: '../music/ ...

What causes the browser to be blocked when using $.getJSON()?

Managing a page that lists hardware devices monitored for customers can be challenging, especially when considering the status of each device. To avoid slow page load times, I have implemented a strategy where the device list is displayed first, and then t ...

Attempting to create an animated carousel slider

I've been working on creating a carousel animation that displays 3 images, slides them to the left, and brings in new ones. I'm feeling a bit stuck and could use some assistance. Check out my work so far on this fiddle link: http://jsfiddle.net/ ...

Concern regarding the width of select forms in Internet Explorer

When coding a select dropdown box using simple HTML, I encountered an issue where it displayed properly in Google Chrome and Firefox, but not in Internet Explorer. In IE, the dropdown became too long and extended out of the box. I attempted to set the wid ...

Disabling pointer-events on material-ui textField input is ineffective

I have a material-ui textField input and I want to prevent the user from entering text by setting the css to pointer-events: none. However, this method does not work as expected. While I am aware that I can use the disabled={true} flag to disable the inpu ...

Trigger a JavaScript notification when a new record is inserted into the database

I'm looking to set up a Javascript alert for users' browsers whenever a new record is added to the database. Here's what I have so far: PHP chatalert.php require("../system/config.php"); $result = mysql_query("SELECT * FROM chat"); $rows = ...

Is there a way to identify when no rows contain specific values in PostgreSQL or node.js and return false?

Here is an example of a table: P Q t f f t f f In SQL, is there a way to return false when querying for t t, but true when querying for t f, f t, or f f? Should this be handled with node.js by first doing a select and then using if-else statements based ...

The functionality of jQueryUI draggable is not functioning properly in Internet Explorer

Can anyone help me troubleshoot why jQueryUI draggable isn't working in IE 8 for me? It's strange because resizable is functioning without any issues. Here is my Javascript code: $(OBJ.cropProfileMiniatureRectangle) .draggable({ containment ...

Breaking up an array into smaller chunks with a slight twist

Here's a straightforward question. I have an array, like this: let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxChunkLength = 3; I am looking to divide this array into multiple arrays as follows: [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, ...

Interactive Sidebar Navigation Content

On my website, I have a navigation sidebar that contains links to all of the main site pages. Each page has the same links, except the link to the current page is not included, making it easy to visually identify which page you are on. Currently, I manuall ...

Encountering a three.js issue while generating a large number of point lights

//////twinkling stars for (let index = 0; index < 1000; index++) { const stargeometry = new THREE.SphereGeometry(1, 24, 24); //create star sphere size const starmaterial = new THREE.MeshStandardMaterial({ color: 0xffffff }); //define star textur ...

What could be causing the entire app to malfunction when the Redux Provider tag is used

I am facing an issue while trying to integrate a React Toolkit app into my project. The error message I encountered is as follows: Error: Invalid hook call. Hooks can only be called inside the body of a function component. This error may occur due to one ...

Is there a way to customize the Master Page in asp.net to resolve the Bootstrap4 dropdown issue? I am encountering an error message that says "Uncaught TypeError: Bootstrap

Hello everyone, I recently encountered an issue while trying to use dropdown in my project after updating to bootstrap 4. It was working perfectly fine before the update, but now I'm getting an error in the JavaScript. The error that pops up when I tr ...

Customize the React Material UI Autocomplete with a unique dropdown menu and a convenient Add Button feature located at

Seeking assistance in creating an autocomplete feature in React using Material-ui, with a unique custom dropdown design including a button labeled Add New Element positioned at the bottom. https://i.sstatic.net/6rY99.png Various attempts have been made t ...

When an item in the accordion is clicked, the modal's left side scroll bar automatically scrolls to the top. Is there a solution to prevent this behavior and

When I first load the page and click on the Sales accordion, then proceed to click on Total reported and forecasted sales, the scrollbar jumps back up to the top The marked ng-container is specifically for the UI of Total reported and forecasted sales He ...

JavaScript array object alerts as empty

Having an issue sending a JavaScript array to my PHP as it appears to be empty []. This is not the usual JSON format that I have worked with in the past. Here is an example code snippet: var blah = []; var letters = ['a', 'b', &ap ...

AngularJS includes a feature where you can dynamically add elements to the DOM by

My goal is to create a dynamic table where clicking on an element will display details in the next line. Here is my implementation: <table ng-controller="TestCtrl"> <tr ng-repeat-start="word in ['A', 'B', 'C']"&g ...

What is the best way to connect to the Microsoft Azure Machine Learning Studio API - through Vue Axios or an Express

I am currently utilizing the following code to establish a connection with a web service. However, I now require assistance in connecting to the Microsoft Azure Machine Learning Studio Api using Vue Axios or Express. Can someone provide guidance? var http ...

Guide on making a Vue.js show/hide button for each element on a webpage

There is a feature to toggle between displaying "more" or "less" text, but currently the @click event affects all elements causing them to show all the text at once. I realize that I need to pass a unique value to distinguish each element, but I am curren ...

What is the best way to send a multi-dimensional array to jQuery UI autocomplete feature?

I am working on an autocomplete field that is connected to an array of friends as its data source. GetFollowing(9, function(response) { if (response) { donor. ...