Guide to building a 4D array in javascript

In my current project, I am utilizing protractor and BDD methodology. One of the tasks at hand involves verifying that the text displayed on a website's body is accurate. This text is dynamic and depends on the selections made in three dropdown boxes.

To accomplish this, I plan to retrieve the text from the website using getText() and compare it with an expected value. The challenge lies in determining this expected text based on the combination of choices made in the dropdown boxes and the specific section of the website being viewed. As a solution, I believe creating a 4D array is necessary.

My question now is how do I declare such an array and assign the appropriate expected texts?

An example structure I have in mind is:

template[0][2][1][0]='this is the expected text';

Answer №1

Creating your own custom code to generate a nested Array list is possible. Here is an example for reference:

function createNestedList(n) {
  var total = 0, levels = n;
  function createNestedList(n) {
    var nestedList = [];
    for (var i = 0; i < levels; i++) {
      nestedList.push(n ? createNestedList(n - 1) : ++total);
    }
    return nestedList;
  }
  return createNestedList(n)[0];
}

console.log(createNestedList(3));

Answer №2

In my opinion, creating a JSON object and passing the dropdown values to retrieve the desired text would make the process more straightforward. Below is an example you can refer to:

let array;
for(let i=0; i<firstDimention.length; i++){
    if (i === 0)
        array = {};
    for(let j=0; j<secondDimention.length; j++){
        // Initializing 2D Array
        if (j === 0)
            array[firstDimension[i]] = {};
        for(let k=0; k<thirdDimension.length; k++){
            // Initializing 3D Array
            if (k === 0)
                array[firstDimension[i]][secondDimension[j]] = {}
            .....
        }
    }
}

If preferred, you can also utilize the same code snippet to initialize an array.

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 refine an array of objects using another array within it

I am currently filtering a group of objects in the following manner: [ { "Username":"00d9a7f4-0f0b-448b-91fc-fa5aef314d06", "Attributes":[ { "Name":"custom:organization", "Valu ...

I am looking to enhance my JSON output using JavaScript

My JSON output appears as follows: {"intent":"P&P_Purchase","value1":{"date1":"30-Dec-19","prd_desc":"NEEM UREA OMIFCO (45 KG)","qty":"18MT","inv_no":"NRKT07003160"},"value2":{"date1":"25-Dec-19","prd_desc":"NEEM UREA IMP (45 KG)","qty":"18MT","inv_no ...

Unable to successfully reset the validity status to true

After implementing server-side validation using the OnBlur event in a form, I encountered an issue where setting the validity of a field to false does not remove the error messages even after setting it back to true. I expected $setValidity true to clear e ...

What is the way to access the Ember global variable 'App' within an Ember CLI application?

As I develop my Ember application with the Ember CLI, I encountered an issue while trying to access the global App variable in order to create and insert a component into my layout. The error message I received was "Uncaught ReferenceError: App is not defi ...

Using Python Selenium to create a login page with Javascript

Attempting to access a page using Python selenium package for certain activities is proving challenging. Despite the following code being written, an error message of "the Class is not found" keeps appearing. To proceed with using send_keys(), it's ne ...

Troubleshooting problem with width in Javascript and Firefox

You can check out the problem over here and control it using the left and right arrow keys. (This works fine in Chrome/Safari, but not in Firefox) I have 9 divs, each taking up the full size of the browser window. These divs act as slides, as demonstrate ...

Executing an objective-c function from JavaScript

How can I call an Objective-C method from JavaScript in a UIWebView, passing and alerting a parameter? I have tried various methods but none seem to work. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationTy ...

conditional statement used in an ng-click function

Can a condition be included within an ng-click directive? I am attempting to prevent form submission if there are any errors present in the form, but I encountered a parse exception. <input ng-click="{{if(profileForm.$valid) updateMyProfile()}}" name= ...

Tally each div individually and display the count within each div, instead of showing the total count across

I have come across various solutions that show the total number of certain special divs, such as: $('.someclass').length However, I am facing a different challenge. I want to sequentially count each div with a numerical sequence. For instance, ...

Setting the date in a datetimepicker using the model

I've encountered an issue while trying to utilize the angular date time picker. It seems that when I load data from the model, the picker does not bind the value correctly. If I manually set the value to a date, it works fine. However, when the data ...

AngularJS traditional navigation rather than using $location.path

While working in controller A, I used $location.path('/home') for redirection. However, I realized that I needed a normal redirect as $location.path does not reload the page. I am aware that in ui-route, we have $state.go({reload:true}) for this ...

Can you provide a brief explanation for this bubble sort JavaScript code?

Can someone please explain to me what the line j<len-i is doing in this bubble sort code? I believe removing -i from that line will still make the program work properly, var arr=[3,5,4,7,8,9,30,0,-1]; function bubble_Sort(arr){ var len = arr.length, ...

Shuffle the elements in an array randomly using Java

I recently came across a discussion on how to randomize or shuffle an array at this link After reviewing the options, I'm not entirely convinced that it's the best approach. My goal is to shuffle the indices of a 3-item array: int numbers[] = ...

Using the Button within Material-Table to Trigger an OnClick Event

Hello! I need assistance with a functionality where clicking the button "xet duyet" should switch to another component (Checkdetail) without displaying its content. Can you provide me with a solution to this issue? Thank you in advance! I have made some a ...

How to eliminate elements from an array in PHP based on their value or key

In my array, the structure is as follows: #items: array:2 [ 8 => 46 46 => 7 ] I am looking to remove the value 46 from the array to end up with an array like this: #items: array:2 [ 8, 7 ] This is what I have tried: $ar ...

Do discrepancies exist between ' and " symbols?

Similar Question: When to Use Double or Single Quotes in JavaScript Difference between single quotes and double quotes in Javascript I scoured this site and did some internet sleuthing (in that order...) trying to find the answer to this burning q ...

Utilize the keyboard's vertical arrows to adjust values as needed

Implement the functionality to increase and decrease the value in a label or p tags using the onkeydown and onkeyup events, without requiring a textbox input. I have come across numerous examples, but they all rely on textboxes. I am looking for a soluti ...

ng-disabled not activating after update to data

Creating an application that allows users to upgrade components using in-game currency has been a challenging endeavor for me. Upon clicking the "upgrade" button, an AJAX request is sent to the server to perform the upgrade. Subsequently, another AJAX requ ...

What is the best way to incorporate a JavaScript file into a webpage specifically for browsers that are using IE 7 or an earlier version?

Is there a way to dynamically include a Java Script file depending on a certain condition? For instance, I am looking for a solution to incorporate a JavaScript file that provides JSON support specifically when the user's browser is Internet Explorer ...

Using Typescript with Protractor for Dropdown Menus

As a newcomer to Protractor, I am looking to automate the selection of a dropdown. While I have some knowledge in JavaScript, I am currently working with typescript. Can someone advise me on how to select the dropdown option based on the text provided? Fo ...