What is the process for designing a function that targets a particular array?

I am currently working on a function that needs to choose a specific array based on the button that triggers it.

For instance:

If a button with id="Anterior" activates the function, it should select the array named plusAnterior:

var plusAnterior = ["p", "b", "t", "d", "ɸ", "β", "f", "v", "θ", "ð", "s", "z", "m", "n", "l", "ɬ", "r", "ɾ"];
var minusAnterior = ["c", "ɟ", "k", "g", "ʃ", "ʒ", "x", "ɣ", "ɲ", "ŋ", "j", "w", "ʡ", "h"];
var toAdd = [];

function addFeature(featureId) {
  if (activeFeatures === 0) {
    toAdd = // plusAnterior selected here
  }
}

The issue I'm facing is that I need the function to be flexible enough to work with any button press. I considered using something like

getElementById("plus" + featureId)
, but I can't since the arrays are not defined in my HTML document.

Your assistance will be greatly appreciated.

Answer №1

When working with JavaScript, you have the flexibility to utilize an object in a manner similar to a key/value array.

var testObject = {
  featureId_a: [1, 2, 3, 4, 5], // etc
  featureId_b: [1, 2, 3, 4, 5] // etc
}

You can then retrieve specific data using a custom method like this:

function addFeature(featureId) {
  return testObject[featureId];
}

This approach allows for easy access to your data. However, it's worth considering other structures such as 2D arrays depending on the nature of your data. Remember to validate inputs by checking if the property exists before retrieving it:

if(testObject.hasOwnProperty(featureId)){
  return testObject[featureId];
}

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 functionality of the Jquery table sorter appears to be malfunctioning, causing it to not

Recently, I included Jquery table sorter in the header of my webpage. However, as soon as I implement the following code in my javascript: $("table").tablesorter(); All my other Jquery functionalities stop working and unfortunately, the table sorter do ...

What causes an ajax request to submit "none" as the value of a dom element?

Seeking assistance! I've encountered a frustrating issue with my simple input box in a form. I am attempting to send the value from this input box to Django using Ajax post, but keep encountering a 500 error that reads ValueError at /rest/ Cannot use ...

To effectively execute a JQuery / Javascript function, it is essential to incorporate both $(document).ready and $(document).ajaxSucces

I have a JavaScript function that is used for basic UI functionality across my site. Some elements affected by this function are injected via Ajax, while others are static HTML. Currently, I have duplicated the function and applied it to both $(document). ...

Obtain the default/initial argument type of typescript for extension purposes

Currently, I am in the process of developing code that automatically generates type hints based on function calls related to GraphQL Nexus tasks. In one of its functions, it requires an anonymous type constructed from the attributes generated automaticall ...

Are AngularJS $scope and Controller as syntax truly interchangeable?

As I delve into learning angularJS, I have found the controller as syntax to be much more readable and easier for me to comprehend, especially coming from the object-oriented world. Despite reading numerous articles and Stack Overflow responses suggesting ...

Basic AngularJS application, however I am receiving {{this is supposed to be the information}}

Building an angularjs app I have set up an asp.net mvc4 application and integrated the angularjs package using nuget. The Layout.cshtml file has been updated to look like this: <!DOCTYPE html> <html ng-app="myApp"> <head> <meta ...

In my React application, I have integrated p5 sketches that constantly loop and repeat

Currently facing a challenge integrating p5 sketches into my React App. I've tried adjusting the z Index of the toolbar to place the p5 elements underneath, but they end up repeating instead. Check out the repository here (sketches can be found in the ...

going to ('/') doesn't lead anywhere

Currently, I am utilizing React Router Dom version 6. As part of my web application functionality, I have created a Redux action named "logoutUser" in my userAction. In the dashboard view file "dashboard.jsx", I have implemented a logout button that should ...

Is there a way to customize setInterval for a specific element?

For some time now, I've been working on creating a typing animation that triggers when links come into view during scrolling. However, I've encountered a problem with jQuery not selecting the correct element or the setInterval objects causing all ...

Animating Plane Geometry with Three.js

I am currently working on a simple 3D "landscape" using three.js for the first time, and I must admit, it's driving me a bit crazy. Despite trying various approaches, I have not been successful so far; in fact, I even managed to crash the browser once ...

After relocating JavaScript code into modules, encountering an error stating 'Cannot read property ClassList of Undefined'

Looking for some guidance in separating my JS code into modules. Everything was working fine when it was all in one file, but after moving it to a module, I'm running into issues. My goal is to make certain elements on my site change visibility based ...

Utilizing Compact Arrays for Efficient Data Retrieval

Dealing with a large image array, I've compressed it for efficiency. To retrieve the pixels in the array, I've devised two methods. def getPixels(a) data = a.unpack('S9s') end def setPixel(array, indexArray, value) index = indexArr ...

Minimal Space Separating Footer Division and Body Element

While there are numerous discussions online about fixing footer alignment issues, I've encountered a unique problem that needs addressing. The issue at hand is a 29px gap between the bottom of the footer and the <body> tag on my webpage, which ...

Demonstrating the transformation of child elements into parent elements through angular 6 ngFor

I have a JSON array dataset where each object may contain nested arrays. In order to display the inner nested array elements as part of the parent array using Angular's NgFor, I need to format the input like this: [{ 'id': 1, 'tit ...

Storing User IP Address in Database with Express and Mongoose

I'm looking for a way to store users' IP addresses in mongoDB by using a mongoose model file for the user. Does anyone have any suggestions on how I can achieve this? Here is an example of the schema for the Users module file: const userSchema ...

Displaying AJAX data in a table format, showcasing 10 rows of information

I am currently working on an ajax function that retrieves data from a database based on the entered information. My goal is to display this information in a table with the following format: System Id | Last Name | First Name | Middle Name | Address Below ...

Angular's minimum date validation is not accurate for dates prior to the year 1901

Any assistance or clarification on this matter would be greatly appreciated. It appears that there may be an issue with my implementation, as otherwise it seems like a significant bug within Angular. Setup Create a form with a minimum date of 0001-01-01 ...

Refresh meta tags in a Next.js/React app

In the process of optimizing a website for SEO, I am facing the challenge of updating the meta description's content dynamically without using any third-party libraries. While I have successfully implemented the functionality, it currently requires a ...

What is the best way to format a text component so that the initial word in each sentence is bolded?

Creating a text component where the first word of the sentence is bold can be a bit tricky. The current solution may result in a messy output like "Tips: favouritevacation" where there is no space after "Tips:". This approach is not very elegant. One pos ...

I have noticed that when I close the Material-UI dialog, it prevents me from interacting with my

After closing the material-ui dialog box, I seem to be unable to click or touch anything on my page. The useState hook is used here: const [open, setOpen] = useState(false); This function is called when handling the dialog close action: const handleClose ...