Reputation: 4466
I am taking a json file and reorganizing it as a object in a new format (Not the same formatting as the json file I am reading, as I want to group variables differently) so I can use it. This requires creating a lot of variables with names that are unknown in advance.
I can dynamically create a new variable in my project like this:
object[variablename]
However, I would like to be able to do things like this
library[musicLibrary.Key].name = musicLibrary.alblum;
and
library[musicLibrary.Key].songs.[musicLibrary.title].name = musicLibrary.song;
Is there any way I can possibly do this?
My current looping code for the json looks like this:
var library = {};
for(var i = 0; i < musicList.songs.length; i++) {
//read json and reorganise it into a object
}
Upvotes: 0
Views: 60
Reputation: 646
for(var i = 0; i < musicList.songs.length; i++) {
var key = musicLibrary.Key;
library[key] = library[key] || {};
library[key]['name'] = musicLibrary.alblum;
library[key]['songs'] = library[key]['songs'] || {};
library[key]['songs'][musicLibrary.title] = library[key]['songs'][musicLibrary.title] || {};
library[key]['songs'][musicLibrary.title]['name'] = musicLibrary.song;
}
Upvotes: 1