unable to iterate through array containing two-value pairs

I need assistance with iterating through the following array

const test = [(18, 20), (45, 2), (61, 12), (37, 6), (21, 21), (78, 9)];

however, when I output it, I get this result:

console.log(test); //[20, 2, 12, 6, 21, 9]

Kindly advise me on the correct way to iterate over this array.

Answer №1

The issue does not lie in the looping (despite the absence of any loop in your inquiry). Your array initially contains values such as 20, 2, 12, and so on. When evaluating (18, 20), the result is 20 because the comma operator discards the left-hand operand (18) and only considers the right-hand operand.

If you intended to create an array of tuples, you should have used [] instead of () for the inner arrays:

const test = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];

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

Export data from Angular Material data table to Excel format

I'm currently utilizing the angular material data table to showcase data in a tabular layout. I have a requirement to add a feature that enables the export of tabular data to an Excel sheet. Unfortunately, I haven't been able to locate any resour ...

Watch as jQuery preloads every select list

There are two select lists, and the second one is dependent on the first. After loading the second list, I want to trigger some alerts. What is the most effective approach to accomplish this using jQuery? Let's say the second list is populated in the ...

Error: Attempting to access 'map' property on an undefined object in a ReactJS application

import React, { useEffect, useState } from 'react'; import axios from "axios"; import './List.css'; import { toast } from "react-toastify"; const ListComponent = () => { const url = "http://localhost:4500& ...

Display popup just one time (magnific popup)

Attempting to display this popup only once during a user's visit. It seems like I might be overlooking something. <script src="http://code.jquery.com/jquery-1.7.min.js"> <link href="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1 ...

Can the line "as is" be included in HTML code?

Can I incorporate the following JavaScript and JSON expressions directly into HTML code? CONTRATE > 50 && SALINC <= 50 || RETAGE >= 50 { "CONTRATE": 0, "SALINC": 0, "MARSTATUS": "single", "SPOUSEDOB": "1970-01-01" } I want to be able to ...

Leveraging 2-dimensional indexes in collaboration with the $geoNear operator

I encountered an issue while attempting to use geoNear with aggregate as I received the following error message: errmsg: "'near' field must be point" The reason for this error is because my location field is represented as [Number]: var locati ...

How can I use JavaScript to disable a table row and then save the selected option in a MySQL database?

I have a PHP snippet that dynamically displays table rows. Each row contains a radio button with "Yes" and "No" options. I have implemented a JS function where, upon choosing an option, a pop-up box is displayed. If the user selects the "Yes" option in t ...

Converting PHP arrays into JavaScript arrays with the help of json_encode()

Is there a way to pass an array from PHP to the JavaScript function console.log()? I'm currently simulating a database and need help with this specific task. Despite not having an array declared in my code, I attempted to use the .getJSON() function w ...

Having trouble routing with express and mongoose, constantly encountering the error message "Type Error: req.params.user is not a function"

I have been struggling with a routing issue for days and cannot seem to figure out where I am going wrong. My goal is to add some fields to the users object through a form. Below is my mongoose schema: var userSchema = mongoose.Schema({ local ...

Focus on all of the individual items except for the one that was just selected

Within the function thirdNavFunction, I am seeking a way to specifically target all instances of the .third-nav classes, excluding the one that has been clicked on. This must be accomplished without utilizing jQuery. How can I achieve this? var thirdNav = ...

The error message thrown by react-big-calendar says: "Attempting to access property '0' of an undefined value",

Issue I encountered an error while trying to integrate react-calendar. The back and next buttons that are supposed to change dates are causing this error: Uncaught TypeError: Cannot read property '0' of undefined Initially, everything was wo ...

Surprising results from combining ng-mousedown with ng-click

I am working with the following HTML structure: <label ng-mousedown="mousedownHandler($event)" ng-click="clickHandler($event)"> <input type="checkbox" /> </label> Within my scope, I have the following methods defined: $scope ...

Switch Between Displaying Individual Javascript Files on a Wordpress Page

In my experience working with a Wordpress page, I've encountered challenges related to JavaScript functionality. While the concept of loading and calling functions in different parts of the site makes sense theoretically, it's something I'm ...

Unleashing the power of dragons through variable links

Having some trouble navigating through this editor, and after searching extensively, I just decided to ask. I'm currently working on a DND character creator using Python. I have a long list of different skills and I want to assign objects from this li ...

The MUI Module is missing: Unable to locate '@emotion/react'

Attempted this solution, but unfortunately it did not work Currently using the latest version of Material-UI Error: Module not found: Can't resolve '@emotion/react' Let's try installing it then.. npm i @emotion/react @emotion/style ...

Adding a directive to create a table header

I'm having trouble with an angular directive that is meant to insert a table header. Unfortunately, instead of being placed inside the thead as intended, it ends up outside of the table element. Here's a link to the codepen: http://codepen.io/sac ...

Using apply quaternion is not a valid function

Can anyone shed some light on this issue I'm having while attempting to set vertices of a geometry using a quaternion? The code snippet I've been using is: geometry.attributes.position.array[x].applyQuaternion(quaternion) However, it doesn' ...

Direct your attention to alternative data sources following the selection of an item in the typeahead search feature

Having some trouble with angular-ui bootstrap: I am using 2 input elements: <input type="text" ng-model="selected" uib-typeahead="state for state in states | filter:$viewValue | limitTo:8" typeahead-on-select="focusSuccessive($item, $model, $label, $ev ...

Is there a way to prevent the omission of zeros at the end in JavaScript when using Number.toString(2)?

I am facing an issue while trying to reverse a 32-bit unsigned integer by converting it to a string first. The toString(2) function is causing the zeros at the end to be omitted, leading to incorrect output. This is my code: var reverseBits = function(n) ...

Use an array to populate a map in Javascript

While working in JavaScript, I am in need of an equivalent of a hash map. After some research, it appears that using the Map object would be the most suitable option. However, my requirement is to set the value of each key-value pair in the Map as an array ...