Creating nested objects in JavaScript can be achieved by using the reduce method to return objects in an array

Looking to nest each element of an array into a new object within another object

let newArr = ['Parent', 'Child']

Desiring the array to be transformed into this structure

  Parent[Child]={}

Seeking a way to iterate through the array and convert it based on its length

Answer №1

['apple', 'banana', 'cherry'].reduceRight((nested, item) => ({ [item]: nested }), {});
// { apple: { banana: { cherry: {} } } }

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 alterations made to a single dropdown option are causing ripple effects across all other dropdowns

There is an add button that continuously adds a div container containing two dropdowns. The selection in one dropdown affects the data in the other dropdown. When the add button is clicked, a second div with both dropdowns is added. However, changing the ...

Adjust the jQuery slider values and then transmit them using AJAX

I´m currently utilizing the jQuery slider plugin and have a specific goal in mind: 1. My aim is to establish different maximum values for the slider based on which li data-value element the user has selected. For instance, if the user clicks on the < ...

I encountered a 404 error when attempting to execute the "npm publish" command

While attempting to publish a package to the npm registry, an error is encountered after running the command npm publish. npm WARN prepublish-on-install As of npm@5, `prepublish` scripts are deprecated. npm WARN prepublish-on-install Use `prepare` for buil ...

Modify marker location as data updates

I am currently utilizing the Google Maps API within a Vue.js project. In my project, I have a table of data that includes positions, and I am looking to update the marker positions dynamically without refreshing the entire card. Below is the code snippet ...

What is the method for altering the look of a button using JavaScript?

As a beginner in web development, I have a question. Typically, I know how to style the submit button when it's created in the HTML page using CSS. However, I'm wondering if it's possible to apply CSS styling to the JavaScript block instead. ...

What methods can I use to ensure that the columns and rows in Vue are of the same size and length

Upon examining the image, it's clear that none of the columns and rows align properly and are all varying sizes. I am relatively new to JavaScript and Vue, so any assistance would be greatly appreciated. As far as my understanding from online resource ...

How can I implement a confirmation modal in Flask for deleting actions?

In my Flask application, I have a page that includes a link to delete a specific item. Each row in a table has a unique ID, which is typically used for deletion. Here is the code snippet for the link: <button type="button" class="btn btn- ...

An error in syntax is being triggered by the Php file being accessed through the <script src="list.php"></script>

We're facing an unusual problem with our online ordering platform script that we purchased a few months ago. It had been functioning perfectly for several months, but now we're encountering an issue! The website page is not loading, and when we c ...

Preventing access to the login page after successfully logging in

Is it possible to restrict access to certain pages after logging in with VueJS? Additionally, how can server validation errors be displayed using the bootstrap invalid-feedback class? Feel free to leave a comment if you need further clarification on my co ...

Navigating a Dynamic entity using a button in AFrame

I've inquired about this topic previously, but I feel my question wasn't clear. My goal is to construct a plinko-style aframe world with a ball that resets to its starting position when clicked. While I prefer to use a button to reset the ball, c ...

Error message: Please provide an expression with const in React JS component

Can you assist me with this issue? I am trying to determine if the user is registered. If they are registered, I want to display the home URL, and if they are not registered, I want to display the registration URL. To do this, I am checking the saved dat ...

Applying scoped styling in Vue.js renders the SCSS ineffective

My SCSS code works perfectly on a simple page where I apply background-color. However, when I make the file scoped, the SCSS doesn't seem to have any effect. It's important for me to keep this component scoped so that the background-color doesn&a ...

Opening external stylesheets in a separate window

Objective: Create a new window, add elements to it, and then apply CSS styles to the elements in the new window using an external style sheet. Issue: While I am able to add elements to the new window and link it to an external style sheet in the head sect ...

[VUE WARNING]: The prop "Items" failed type check. It was supposed to be an array but instead received a Promise

I am completely new to the world of node technology and I am challenging myself with a project to enhance my skills. So far, I have seen some success (perhaps by luck), but now I seem to be stuck. The client side involves 'axios, vue, vue-router, vuet ...

Using data from an API, I am implementing JavaScript validation for my dropdown select menu

Using an API, I am able to access information about the city's subway stations through a select option. Currently, I can only display details about one station (Balard). However, I would like to be able to display information about other stations tha ...

Leveraging Firebase in Electron Applications

I've been experimenting with integrating Firebase into Electron. However, I've run into a roadblock when trying to set it up similar to how I would on a regular web page. Since Electron pages are hosted locally and lack a hostname, the usual setu ...

Angular controller fails to fetch scope variable in subsequent view

I'm currently working on implementing a login feature that directs a validated user to a personalized HTML view that greets them with their name. The initial view is a login page. When the user clicks the "Log In" button, the ng-click function is sup ...

Need to swiftly modify values of css attributes?

Here is a snippet of the code I am working with: <div></div> <button> Go </button> div { width: 50px; height: 50px; border: 1px solid #ccc; } var bgs = ['red', 'blue', 'yellow', 'green&apo ...

Is there a way to dynamically create a property and assign a value to it on the fly?

When retrieving data from my API, I receive two arrays - one comprising column names and the other containing corresponding data. In order to utilize ag-grid effectively, it is necessary to map these columns to properties of a class. For instance, if ther ...

Trouble updating Kendo DropDown in Internet Explorer

Having an issue with updating a Kendo DropDownList through a javascript function. It works fine in FireFox and Chrome, but not in Internet Explorer. @(Html.Kendo().DropDownList() .Name("myDDL") .HtmlAttributes(new { style = "width: 320px" }) . ...