The analogous code in Javascript for resizing and keeping data intact, similar to V

Currently, I am in the process of converting some older VBScript to Javascript for an ASP.NET project and there is a particular line that has me a bit stumped, as I am not entirely clear on its purpose.

The main function of the application is to allow users to input a new employee number into the database and then assign user permissions. The code I inherited is quite messy, but I am determined to get it functioning properly in Chrome.

Here is a snippet of the code that I have successfully translated so far:


if(form1.txtEmpno.value != ""){
var oOption;
oOption = document.createElement("OPTION");
oOption.text=form1.txtEmpno.value;
oOption.value=form1.txtEmpno.value;
form1.lstActive.add (oOption);
oOption = document.createElement("OPTION");
oOption.text="";
oOption.value="";
form1.lstPerms.add (oOption);
redim preserve arrUsers(1,ubound(arrUsers,2)+1);
arrUsers(0,ubound(arrUsers,2)) = form1.txtEmpno.value;
arrUsers(1,ubound(arrUsers,2)) = "";
form1.txtEmpno.value = "";
oOption = null;
}

And here is the specific line causing uncertainty:

redim preserve arrUsers(1,ubound(arrUsers,2)+1);

Answer №1

According to MSDN, the

ReDim [Preserve] varname(subscripts)
statement is used to resize a dynamic array that has been declared using a Private, Public, or Dim statement without dimension subscripts. It can be repeatedly used to change the number of elements and dimensions in an array.

If the Preserve keyword is used, only the last array dimension can be resized without changing the number of dimensions. This means if there is only one dimension, it can be resized as it's the last one. However, for arrays with two or more dimensions, only the last dimension's size can be changed while preserving the contents of the array.

In JavaScript, arrays have different semantics compared to VBScript arrays. They are more similar to vectors than true arrays. Unlike VBScript, JavaScript does not support true N-dimensional arrays; instead, staggered-arrays (arrays-within-arrays) are used. Therefore, syntactically converting VBScript code to JavaScript may not be straightforward.

The relevant VBScript code snippet provided involves a 2-dimensional array named arrUsers. To convert this into a staggered array in JavaScript, modifications need to be made to accommodate the differences between the two languages.

A simpler approach in JavaScript could involve utilizing function-properties like push and pop for array manipulation:

var arrUsers = [ [], [] ]; // creating an empty, staggered 2-dimensional array
...
arrUsers[0].push( form1.txtEmpno.value );
arrUsers[1].pop();

If the array is primarily used to store and represent data within an internal model, leveraging JavaScript object prototypes might provide a more descriptive and efficient solution:

var User = function(empNo, name) {
    this.employeeNumber = empNo;
    this.name = name;
};

var users = [];
users.push( new User(1, "user 1") );
users.push( new User(23, "user 23") );

...

for(var i = 0; i < users.length; i++ ) {
    alert( users[i].name );
}

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

Are there any alternative methods to refresh the Google Search Result for my website? (Meta tags are ineffective)

I'm facing an issue with the Google search result of my website where it's displaying old text instead of what I intended. I've tried using meta tags like nosnippet and description, but the problem persists. Can anyone suggest resources or ...

Menu that changes dynamically according to the locations listed in a multidimensional array

Looking to create a dynamic menu based on locations stored in an array of arrays. The goal: Create a menu structure like this: Location 1 |_ sub-location 1 |_ sub-location 2 |_ floor 1 |_ floor 2 Location 2 |_ sub-location 1 etc, etc. ...

Rearranging a JSON Object post editing

Currently, I am working on a project where I need to display and sort a list of items based on a JSON object. This includes sorting by both string values and integers within the object. So far, I have been successful in listing and sorting the items by st ...

Issue with Html.DropDownListFor not updating values in C# MVC

I need three dropdown menus on my website. Here's how they should function: The user must make a selection from the first dropdown in order to populate the second dropdown. The same goes for the second and third dropdowns. The Index method is respon ...

Initiate an Ajax request when beforeunload/unload event occurs

Situation: Currently, I am developing a system for event registration which involves users selecting dates from a calendar to book, providing information in input fields, and making payment to secure their booking. One crucial requirement is that each date ...

Using CanvasTexture to apply as a texture map on a 3D .obj model that has been

I recently imported a .obj file into my project using the following code: OBJLoader.load('/models/model.obj', m => { let model = m.children[0]; model.material = new THREE.MeshBasicMaterial(); ...

Eliminating the selected option from the dropdown menu after it has been added with Angular

HTML Code <!doctype html> <html ng-app="plunker"> <head> <meta charset="utf-8> <title>AngularJS Plunker</title> <link rel="stylesheet" href="style.css"> <script> document.write("<base href=&b ...

CORS headers not functioning as expected for Access-Control-Allow-Origin

Can someone help me figure out how to add Access-Control-Allow-Origin: 'http://localhost:8080' in Node.js and Express.js? I keep getting this CORS error: Access to XMLHttpRequest at http://localhost:3000 from origin 'http://localhost:8080&ap ...

Reveal and conceal information with a customized web address

I have a PHP and MySQL blog that I want to display in a div using show and hide JavaScript functions. Everything works fine with other divs, but the issue arises when clicking on a vanity URL causing my webpage to refresh every time it's clicked. The ...

Altering the "src" property of the <script> tag

Can the "src" attribute of a current <script> element be altered using Jquery.attr()? I believed it would be an easy method to enable JSONP functionality, however, I am encountering difficulties in making it operate effectively. ...

Introducing the latest function for combining arrays: Array Fusion

I need help with consolidating data from this table: id | array 1 | {8,8,8,x,u,x,x} 2 | {8,8,8,x,8,x,x} ...|... n | {8,u,u,x,u,x,x} This table represents the time each employee worked (columns in the array represent days of the week, u and x ind ...

How to determine the frequency of a specific word in a sentence using Angular 5

I need help finding a solution to count how many times a word appears in sentences. Input: k = 2 keywords = ["anacell", "cetracular", "betacellular"] reviews = [ "Anacell provides the best services in the city", "betacellular has awesome services", ...

Creating Highcharts series with dynamic addition of minimum and maximum values

I am currently working with a Highcharts chart that includes multiple tabs allowing users to display different data on the graph. I am dynamically adding series for each of these data points, which has been functioning well. However, I have encountered an ...

I encountered an issue with rendering static images when attempting to package my node-express app with pkg

Struggling to display an image from the public folder in my express app? I could use some guidance on configuring the path to properly render images or css files within the public folder when creating an executable file using pkg. Here's a snippet of ...

Is it possible to determine if there are an odd number of occurrences of 0 within an array using bit manipulation?

In the realm of integer arrays, there exists a fascinating concept: XORing all elements in an array where every element occurs an even number of times except for one will reveal the elusive odd-occurring number. For example: { 1, 1, 2, 2, 3 } 1 ^ 1 ^ 2 ...

Continuous background image motion descending from the top

Can anyone help me modify this JavaScript code so that the background image moves from top to bottom instead of left to right? <script> $(function(){ var y = 0; setInterval(function(){ y+=1; $('body&a ...

"Utilize jQuery to create a new row when an image button is clicked

I am looking for a way to duplicate the existing <tr> every time I click on the + button, represented by <i class="fa fa-plus" aria-hidden="true"></i> Here is the structure of my <tr>. How can I achieve this using jQuery or JavaScr ...

A guide to connecting keyboard events to div elements within a React application

Currently working on a basic react project to gain some practical experience with the library. I aim to develop a simple user interface with blank spaces to be filled in by typing via keyboard input. Here's a glimpse of my progress so far: function ...

Issues encountered when using .delay() in conjunction with slideUp()

Issue: The box is not delaying before sliding back up on mouse out. Description: Currently, when hovering over a div with the class ".boxset" called "#box", another div "#slidebox" appears. Upon moving the mouse away from these two divs, #slidebox slides ...

What is the best method for styling arrays displayed by React in version 15 using CSS?

React v15 has made a change where elements of arrays in JSX are now rendered as <!--react-text:some_id-->text<!--/react-text--> instead of spans. I've searched for a solution but haven't been able to figure out how to apply CSS styles ...