JS assigning values from one array to another existing array

Can it be done? I'm attempting to achieve the following:

var foo = 'foo';
var bar = 'bar';

var x;
var y;

var array1 = [x, [foo, y]];
var array2 = [x, [foo, bar], [foo, y]];

console.log(array1[0]); //undefined
console.log(array1[1][1]); //undefined

console.log(array2[0]); //undefined
console.log(array2[2][1]); //undefined

array1[0] = 'working';
array2[0] = 'working';

y = 'hello';

console.log(array1[0]); //working
console.log(array1[1][1]); //undefined

console.log(array2[0]); //working
console.log(array2[2][1]); //undefined

While it's easy to assign a value when you know where the binding value is located, x in this scenario, it becomes difficult to bind a value to values with unknown location, y in this case.

Any ideas or suggestions? Thank you.

Answer №1

If you're looking for a solution, consider using a wrapper like this:

let obj1 = {};
let obj2 = {};

let arr = [obj1, [apple, obj2]];

console.log(arr[0].value); // undefined
console.log(arr[1][1].value); // undefined

obj1.value = "sun";
obj2.value = "moon";

console.log(arr[0].value); // sun
console.log(arr[1][1].value); // moon

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

Exploring the Depths of Javascript Variable Scope

bar: function () { var cValue = false; car(4, function () { cValue = true; if (cValue) alert("cvalue is true 1"); }); if (cValue) alert("cvalue is true 2"); } car: function (val, fn) { fn(); } I have encountered a similar is ...

Next.js App experiences a 404 Error when attempting to access the Auth0 Endpoint "api/auth/me"

Following a tutorial, I integrated my Next.js App with Auth0 successfully. However, after logging in and out, I encountered an issue where the user object is not returned when trying to display user information on the page post-login. Both the Profile.js p ...

Use HTML to showcase an image that dynamically changes based on the outcome of a query function

Hello there, I hope my inquiry is clear enough. I apologize for reaching out as I am unsure where to begin and what exactly I should be focusing on. Currently, I have an image displayed in an HTML page like this: <div id="tag_sunrise_sunset"><p ...

AngularJS xeditable: sending updated data to the server

I am currently utilizing AngularJS to display products in a table for my users. Users have the ability to filter the table using categories or keywords. However, they should also be able to edit the product information within the table, such as product nam ...

Are there any reliable alternative libraries in Javascript for supporting newer functions?

Is there a reliable alternative JavaScript library available that can generate functions for features not supported by older browsers? I'm currently working with a library that relies on functions only found in newer browser versions, and I'd pre ...

resizing a big image to perfectly fit the width of the screen with the help of

Is there a way to use AngularJS to automatically adjust the size of a large image to fit the screen on various devices (responsive), similar to how it is done on this website? Are there any Angular directives available for this purpose, or would I need to ...

Is React invoking a function?

Just starting out with React and experimenting with lists. Not sure why my code isn't working as expected - when I call the method, it just shows plain HTML. I've already tried wrapping it in <Liste /> tags, but that didn't help eithe ...

Trigger a JavaScript function from ASP.NET code-behind

Here is a JavaScript function I've created: function returnToParent() { var oArg = new Object(); var oWnd = GetRadWindow(); oArg.ReturnValue = "Submit"; oWnd.close(oArg); } This is how I call the func ...

What is the best way to update data in real-time following a POST request?

I have a table displaying data. While the POST and GET requests are functioning properly, I am only able to see the values after insertion upon refreshing the page. xyz.service.ts createCategory(options) { return this.http.post<{ message: string } ...

JavaScript code that retrieves an array containing only the deleted images from the information obtained from the edit product page

Currently, I am working on an edit product page in react with a node backend. The situation is as follows: Initially, the product had 4 images (a.png, b.png, c.png, d.png). I have made updates by removing the a.png image and adding a new image e.png. So ...

Incorporating CCPoint into CCArray

In my quest to select a random CCPoint from the CCArray, and subsequently exclude that point to avoid duplication, I encountered an issue with the following code snippet: myArray->addObject(pos1); In this scenario, pos1 denotes a CCPoint, while my ...

Tips for preventing redundant HTTPInterceptor requests when transitioning between RxJS mergeMap operations

My system utilizes two JWT tokens: a Refresh Token (expires after 7 days) and an Access Token (expires after 15 minutes). These tokens are securely stored on httpOnly cookies and can be accessed via the server. The refresh method generates a new token and ...

Tips for changing ES lint rules in a specific file within a React application

Currently, I am incorporating some older code into my React application. Webpack is generating numerous errors related to "no-restricted-globals" and "no-undef," which is preventing successful compilation. I am interested in finding a way to bypass this f ...

One Versatile Ajax Function for Accessing Various PHP Pages

On my index.html, there are buttons displayed below: <button value="about_us.php" onClick="fNt(value)">About Us</button> <button value="faq.php" onClick="fNt(value)">FAQ</button> <button value="contact_us.php" onClick="fNt(val ...

What is the best way to incorporate raw HTML code into a .jsx file within a NextJS website?

I need to integrate Razorpay code into my NextJS website, but it's currently in pure HTML format. For example, the code looks like this: <form><script src="https://cdn.razorpay.com/static/widget/subscription-button.js" data-subscrip ...

Ways to dynamically update the value of an object property within reactJS state

In the scenario where a component holds state like so: this.state = { enabled: { one: false, two: false, three: false } } What is the proper way to utilize this.setState() in order to set the value of a dynamic property? An attempt such ...

PHP Dropdown List - Default option should be set to "all" (or "Alle")

My website displays data to users based on the State they reside in, with a filter provided through a drop-down list allowing them to select any specific State or view data from all States. Currently, the default selection shows the user data from their ow ...

Problem with HTML relative paths when linking script sources

Having trouble with a website I constructed using Angular. The problem lies in the references to javascript files in index.html. The issue is that the paths in the HTML are relative to the file, but the browser is searching for the files in the root direct ...

Retrieving data from a <span> element within a webpage created using ReactJS

I am completely new to the world of web design and development, so there may be some mistakes in my request. Essentially, I have a webpage that contains numerous span tags like the following example: These span tags are part of a significantly large DOM t ...

Eliminate the final two digits from each element in the array if they exceed two digits

A challenge I am facing involves an array with multiple elements. My goal is to remove the last two digits from any element in the array that consists of 3 or 4 digits, while leaving elements with 1 or 2 digits unchanged. var test =[ 2, 45,567, 3, 6754]; ...