Generate variables for an object on the fly

As I work with a json file, my goal is to transform it into an object with a different format than the original file so that I can better utilize the data. This involves creating numerous variables with unknown names beforehand.

To dynamically create a new variable in my project, I use the following syntax:

object[variablename]

However, I am interested in more complex operations like:

library[musicLibrary.Key].name = musicLibrary.album;

and

library[musicLibrary.Key].songs.[musicLibrary.title].name = musicLibrary.song;

Is there a way for me to achieve this level of functionality?

Currently, my loop code for processing the json file looks as follows:

var library = {}; 

for(var i = 0; i < musicList.songs.length; i++) {
//read json and reorganize it into an object
}

Answer №1

for(var j = 0; j < playlist.songsList.length; j++) {
    var genre = songLibrary.Genre;
    music[genre] = music[genre] || {};
    music[genre]['name'] = songLibrary.albumName;
    music[genre]['songs'] = music[genre]['songs'] || {};
    music[genre]['songs'][songLibrary.trackTitle] = music[genre]['songs'][songLibrary.trackTitle]  || {};
    music[genre]['songs'][songLibrary.trackTitle]['name'] = songLibrary.songName;
}

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 Organization of Tabs in the AngularJS ui-select Component

As a newcomer to angularjs, I am currently working on a web application using the ui-select library for select2-style dropdowns. While developing my forms with appropriate tabindex values, I noticed a user experience issue with tab ordering. When tabbing ...

Exploring CakePHP 3's capabilities with JSON response: Enhancing response data format by connecting with related tables

I am working with two tables, each with their own set of attributes: Sessions id staff_id Staff id firstname lastname When a link is clicked, it triggers a JavaScript function to open a modal. An AJAX call is then made to retrieve data in JSO ...

Exploring the process of retrieving cookies on both the client and server side with Next.js 13

One of the functionalities in my code is a custom function called myFetch, which serves as a wrapper for fetch to include a token in the header. import clientCookies from "js-cookie"; import {cookies} from "next/headers"; const myFetch ...

Discovering a method to identify a minimum of two pairs of letters that are not adjacent

After using 'aabcc'.match(/(\w)\1+/g) to find repeated pairs of letters, I'm now looking to update my regex to specifically identify non-adjacent pairs. Any suggestions on how to achieve this? For instance: The string 'aabcc ...

Is it possible to adjust the display size of a component in Angular using a selector?

Currently, I'm facing an issue with the size of a selector, <component-selector></component-selector>. It seems to be displaying too large and cutting off the right side of the contents. I've attempted the following code: <div sty ...

Using Angular's ng-repeat directive without the need for an HTML element

I need help with a foreach loop in Angular that I am struggling with. Here is what I am trying to achieve: <ul> <li ng-repeat="g in groups"> <li ng-repeat="c in g.commands">{{c.text}}</li> <li class="divider" ...

Unable to trigger mouse event for a div that is positioned on top of an image

Similar Issue: Problem with IE: Transparent div over an image not triggering CSS:hover Greetings, I am currently experiencing a problem with the mouseover and mouseout events for a div that overlaps with an image. I am trying to display hotspots (high ...

Utilizing the power of AngularJS in conjunction with requireJS

Currently, I am diving into a tutorial on the integration of AngularJS with RequireJs. However, I am finding it challenging to grasp the concept. In the tutorial, the author introduces a file named app.js and includes the following code snippet; define(f ...

What is the proper method for utilizing colspan within the footerData of a jqGrid?

Are you looking to customize the footer of your jqgrid as shown in the example below? I am trying to set up a custom footer for my jqgrid similar to the one displayed above. I have already enabled the footerrow:true option and used $self.jqGrid("footerDat ...

What is causing my HTML select element to prevent me from accessing its options?

In my HTML, I have a select element that is populated with options using JavaScript. <select name="send_hour" class="input-mini" id="send_hour"></select> I am trying to access the length of the options using JavaScript. $("#send_hour").optio ...

Filtering an Array of Objects on the Fly in Vue.js

I'm currently working on a Vue.js app where I need to dynamically apply filter values to an Array of objects based on their field values. Each object in the Array has various fields that I want to filter by. The challenge is that each field can have m ...

Adjustable textarea with automatic height based on content and line breaks

Imagine you have a textarea with text that includes line breaks. If you style it like this: textarea { height: auto; overflow: hidden; resize: none; } Upon loading the page, the textarea will only adjust its height based on the content until it enc ...

Turn off Drag and Drop Feature for jQuery File Upload

Currently, I have implemented an ajax file uploader on my website using the following plugin: https://github.com/blueimp/jQuery-File-Upload On one of my pages, there are two controls for uploading files - one for images and another for documents. Both con ...

Navigational link behavior in the React component with the <tr> element

I am in the process of developing a React application with a component that shares a resemblance to the following: class TableRow extends React.Component { constructor(props) { super(props) } render() { const handleClick = () ...

What is the best way to retrieve a username by using a checked checkbox in JavaScript or jQuery?

i HTML <div id="notApprovedUsers"> </div><button onclick="approveUsers()">Approve</button> </div> i enjoy working with checked users passed in the userList parameter. ...

Displaying only the matched column in an Angular table

I am attempting to display only the matched columns in a table, but I am unsure of how to achieve this. If anyone has any ideas or solutions, please help me out. app.component.ts: columnsOnly = ['name', 'id', 'rank']; ite ...

Incorporating an express server into the vue-webpack-boilerplate for enhanced functionality

Recently, I got my hands on the vue-webpack-boilerplate from GitHub, and so far, it seems pretty impressive! This is my first time working with webpack and ESlint, so I'm eager to learn more. However, I have a question about integrating an express ba ...

Manipulating the position of objects in Three.js using WebGL

Currently, I am exploring WebGl and Three.js to work on a project. Unfortunately, I seem to be encountering an issue regarding retrieving the position of a custom mesh that I created. Each time I attempt to obtain the position, I consistently receive value ...

Improving user experience through real-time form validation in CodeIgniter with the

As I am working on a form with fields such as Fullname, Password, and Mobile no, each field is initially displayed individually on the page. When a user clicks on a button, the next field should be displayed while setting AJAX validation on it. The goal is ...

Trouble with implementing an onclick event listener in a foreach loop

While generating and adding HTML in a for loop, I noticed that adding onclick events within the same loop is not functioning correctly: items.forEach(item => { itemHtml = `<div class="${item.id}">${item.id}</div>`; $(".it ...