Creating a JSON object with nested arrays by adding items in JavaScript

I have an array of names and another one with JSON data accessible through a method. I attempted to carry out the following:

let out = [];

for (let i = 0; i < data.length; i++) {
    for (let j = 0; j < names.length; j++) {
        let feed = {[names[j]] : getData(data[i])};
        out.push(feed);
    }
}

The expected result would look something like this:

[{"jonas": {"a": 1, "b": 2}}, {"lara" : {"a": 73, "b": 0}}, "jakob": {"a": null, "b": "something"}]

However, what actually occurred was an error: unexpected token '[' The problem stemmed from the '[' at ...let feed = {names[ /←this here/ j]...

Answer №1

If you want to utilize a variable as the key for an object, remember to put it inside [], consider implementing this code snippet

const product = {[brands[i]]: retrieveData(info[j])};

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

Error message "Function pointer is not a valid function" encountered in Angular version 1.2.25

I am encountering this error sporadically and without any specific pattern. Previously, the page was functioning correctly, but now this error is causing disruptions. Is there a recommended method for debugging or tracing the source of this problem? Err ...

Interactive form control for location details including country, state, district, and town

I am struggling with adding dynamic form controls on dropdown change. I have been able to add them, but encountered an error preventing me from retrieving the value in 'formName.value'. The specific error message states: "Error: There is no Form ...

Implement a dynamic loading feature using AJAX, pushState, and Jekyll for a

In an effort to create a standard template for our studio's Jekyll sites development, I am aiming to implement page animation transitions. My plan involves loading different content from each page using AJAX and utilizing pushState to update the URL a ...

Utilize lodash to locate the key of an object with deeply duplicated values

Looking at the object provided below: var data = { 2: [[1,2],[3,4,5],[6,7]], 3: [[1,2],[3,4],[6,7]], 4: [[1,2],[3,4,5],[6,7]], 5: [[10,2],[3,4,5],[60,7]] } I am interested in retrieving the key 2 from the object as its ...

Whenever I attempt to invoke a generic handler through cross-domain using ajax, the data returned turns out to be undefined upon the successful

Whenever I use Ajax to call a generic handler across different domains, the data comes back as undefined in the success callback. function getUserInfo() { currentUser = null; $.ajax({ type: 'GET', ...

React dynamic dropdown selection based on previous dropdown values

I have implemented 3 react input fields that are populating data to a useState Hook <FormControl fullWidth> <InputLabel>Primary Goal</InputLabel> <Select ...

Using JsGrid to efficiently load nested object data into a table

Currently, I am in the process of developing a web project using Django and implementing jsGrid. However, I have encountered an issue for which I cannot seem to find a solution. The problem lies with my nested JSON data that is generated by merging record ...

JavaScript - Transferring Files across Folders

I manage three folders: 'Received', 'Successful', and 'Error'. When a new file is added to the 'Received' folder, my system processes it. Successfully processed files are moved to the 'Successful' folder, w ...

Triggering a 'RegisterStartupScript' call resulted in an 'Object expected' error

Something strange is happening here. Let me explain the situation: I have a page with a usercontrol used for popups. This usercontrol has a placeholder where I can load any other usercontrol dynamically inside the popup. Everything works smoothly thanks ...

Surprising Behavior of React's OnClick Event

Custom Component function ProductCard ({image, name, stats, id}){ let dispatch = useDispatch() let quantity = 1 return ( <> <div className="product-card" ...

What about connecting mapStateToProps with a specific ID?

Currently, I have a basic function that fetches all Elements const mapStateToProps = ({elements}) => { return { elements: getElementsByKeyName(elements, 'visibleElements'), }; }; I want to modify it to something like this c ...

What is the best way to stop a jQuery function from applying titles extracted from the first row th's in thead's to multiple tables in a document?

My circumstances: I am new to jQuery and Stack Overflow, seeking assistance for my website project. The challenge: Building a website using Bootstrap 3.3.6 with intricate data tables that need to be stacked dynamically on mobile devices using CSS. It is c ...

Deserializer for Gson with recursion functionality

Creating a custom Gson deserializer that can recursively go into objects and deserialize them with the same concept is my current challenge Let's start by creating a JSON object for demonstration: { "name": "Hello world!", &qu ...

How Can I Separate URL Query Parts into Different Arrays by Distinguishing Keys and Values?

When working with the following URL: http://example.com/cat/subcat?var1=value1&var2=value2 I am tasked with extracting the keys and values from the query part into separate arrays. For example, the $query_string array should include these values: var ...

Issues with click events in the navigation menu

How can I make my menu close when clicking on other parts of my website, instead of opening? I know that I should use a click event for this, but when I implemented a click event, my menu encountered 2 unwanted problems: 1- Whenever I clicked on a menu i ...

What is the best way to transfer variables and functions between JavaScript and Python seamlessly?

I am in the process of building a website using HTML, CSS, and JavaScript, and I am in need of an AI-powered chatbot. I have a Python file that contains the necessary logic for the chatbot (AI, NLTK). Inside the Python file, there is a function called "res ...

Mouse hovering over the JS slider activates the sliding functionality, while removing the cursor

Hi there, I'm encountering some issues with the JS clients slider on my website. I need to pause it when the mouse is over and resume when the mouse leaves. I've double-checked the code but for some reason it's still not functioning properl ...

Enhancing the accessibility of Material UI Autocomplete through a Custom ListboxComponent

I have developed a custom ListboxComponent for the MUI Autocomplete component in MUI v4. How can I ensure that it meets the necessary accessibility requirements, such as navigating through options using the arrow keys? <Autocomplete ListboxComponent ...

What is the process of serializing an ExpressionSet into JSON format?

I am looking to JSON serialize an expressionSet. I attempted the following: # Creating expression set based on the provided link library("Biobase") ExpressionSet() ExpressionSet(assayData=matrix(runif(1000), nrow=100, ncol=10)) # Updating an existing E ...

What is the best way to record and share a video with jquery and laravel?

Is there a way to grant users access to record videos within an application and then upload them after previewing? I've been able to successfully record and download a video, but now I'm unsure of how to proceed with uploading it to the server. ...