Retrieve an object using the coordinates (x,y) from a given list

I am working with a two-dimensional array, matrix[2][2], that I have stored in a list:

var list= ["a","b","c","d"];

In addition to the list, I also have two specific coordinates:

var x = 0;
var y = 1;

My goal is to retrieve the element at position (x,y) from the list, for example: (x,y)==(0,1) which corresponds to the letter c.

Can anyone advise me on how to achieve this?

The fictitious matrix depicts the following structure:

a b
c d

Answer №1

The equation you need to use is:

position = row * totalColumns + column;

In this equation, row represents the row index and column represents the column index.

For your specific scenario (assuming there are 2 columns), the calculation would be:

let element = array[row * 2 + column];

Note: In my explanation, I refer to column as the horizontal position and row as the vertical position, which I believe is the more precise terminology. If your setup has it the other way around (column is the row and row is the column), then simply interchange column and row in the formula.

Example:

const array = ["apple", "banana", "cherry", "date"];


let column, row;

column = 0; row = 0;
console.log("(" + column + ", " + row + ") = " + array[row * 2 + column]);

column = 1; row = 0;
console.log("(" + column + ", " + row + ") = " + array[row * 2 + column]);

column = 0; row = 1;
console.log("(" + column + ", " + row + ") = " + array[row * 2 + column]);

column = 1; row = 1;
console.log("(" + column + ", " + row + ") = " + array[row * 2 + column]);

Answer №2

Give this a shot:

function access_xy_values(my_list, x_index, y_index)
{
    var temp = []  
    temp.push(my_list.slice(0, 2), my_list.slice(2, 4))
    return temp[x_index][y_index];
}

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

The Javascript function is malfunctioning, unable to assign the 'onclick' property to null

Here's the code snippet I'm working with: var exit = document.getElementById("exit"); exit.onclick = function() { "use strict"; document.getElementById("fadedDiv").style.display = "none" ; }; However, when I check the console, it shows ...

Ways to trigger various functions upon alteration of minimum and maximum values in a range slider bar

I am currently working on an app that features a range seek bar. My objective is to invoke a method passing the minimum value of the bar if it has been changed, and likewise to trigger another method passing the maximum value if it has been changed. Below ...

Transforming Attribute Directives into Elements in Angular 2

I've been trying to wrap my head around this Attribute Directive feature in Angular 2. Initially, I thought that directives could only be created using Attributes, but after some experimentation, I realized that I can also create directives using Ele ...

Move files in Node.js without removing the original source directory

Currently, I am facing an issue while attempting to transfer files from one directory to another using the mv module. The problem arises when the files are successfully moved, but the source directory is automatically deleted. My intention is for only the ...

Determine in Node.js if a word has been altered or has an additional letter compared to the previous word

I'm currently developing a feature for my Discord bot that allows users to play a word game. The objective is to input words that either change one letter from the previous word or add a letter. To implement this, I am utilizing the following function ...

Binding hover and load events using jQuery on elements that are dynamically generated

One should note that the click event can be successfully bound to an element with the class name keybox, even if this element is dynamically generated. The code for this would look like: $('body').on('click', '.keybox', funct ...

What is the best way to stack col-xs-6 elements instead of having them side by side?

My form fields are currently set up using 3 col-xs-6 classes, but I'm not seeing the desired layout. I am aiming for: | col-xs-6 | | col-xs-6 | | col-xs-6 | However, what I am getting is: | col-xs-6 | col-xs-6 | | col-xs-6 | I understand that th ...

Removing byte arrays from software and RAM in C#

I am developing a program that requires one of its functions to repeatedly call a custom function to add different parts to an array, in order to construct the message to be sent. However, I have encountered a limitation with C# where byte arrays (and poss ...

How to combine arrays in Laravel 5.3 using 2 matching keys?

How can the merging of two arrays efficiently result in a third array with the given information? productData $productData = [ { "product_id": 4, "type": "electronic", "name": "monitor", "specs": { "HDMI": true, "VGA": false ...

Guidance on sending Ajax response to JavaScript function in OctoberCMS

In the HTML file of my component, I have created a form. When I submit the form, it triggers an AJAX call to an action defined in the component. Currently, my code is functioning properly and I am receiving a response. However, I now need this response to ...

Can someone confirm if a user has administrator privileges?

Is it feasible to determine if members of a discord server possess administrator privileges in a looping structure? I am aiming to prohibit individuals who hold a role below my bot in the server that belongs to me and my companions. How can I achieve this? ...

Storing a chosen avatar in a database: A step-by-step guide

I am looking to display a selection of 10 avatars on my website for users to choose from. There will be no option for users to upload their own avatar. Users will simply click on the avatar image they want and then click the "done" button. The selected ava ...

The command 'var' is not recognized as an internal or external command within npm

I need assistance with installing the histogramjs package. The command npm i histogramjs successfully installs it, but when I attempt to run the code using var hist = require('histogramjs'), I encounter an error in the command prompt: 'var& ...

The search functionality is malfunctioning within the JavaScript table

I am working with a table that I want to filter based on input values. However, the current filtering function is not working as expected. I have utilized an onkeyup function for the filtering process. For more details and a live example, check out the jsf ...

How to implement an Angular Animation that is customizable with an @Input() parameter?

Have you ever wondered if it's possible to integrate custom parameters into an Angular animation by passing them through a function, and then proceed to use the resulting animation in a component? To exemplify this concept, consider this demo where t ...

Modify the color of a div element in React when it is clicked on

My goal is to change the color of each div individually when clicked. However, I am facing an issue where all divs change color at once when only one is clicked. I need help in modifying my code to achieve the desired behavior. Below is the current implem ...

Sorting a select element using Javascript/JQuery while taking into consideration the label attribute present in the options

It came to my attention that the jQuery options and examples only covered the text and value attributes, causing errors when attempting to add the label attribute. I am looking for a way to include the label attribute in the sorting process, especially whe ...

What are the steps to utilize webpack for refreshing the script tag in a jade file?

Can webpack be used to automatically update the script tag in a jade/pug template to point to the minified and cache-busted JS bundle? Most examples I've come across demonstrate how plugins can be used to convert the jade template to HTML, but I am us ...

Storing the many-to-many field when creating an object in Django DRF

Excuse me if this may seem like a straightforward question. I have looked for various solutions, but none of them seem to address my specific issue. I am dealing with a model that has a many-to-many field relationship. Whenever I create a new object throu ...

A guide on transferring data to an external JavaScript script that is asynchronously loaded

I came across this solution on Stack Overflow here to load JavaScript asynchronously. However, I am wondering how can I pass some data to the external file? This is my current code: <script id="js-widget-o2ba1y" type='text/javascript'> ...