In this tutorial i am going to explain how to create a JSON Structure dynamically. I had an requirement where i wanted to create an object dynamically to store the information on tab(Icon Tab Bar) and No of list that was dynamically created within the Tab to track them. So i had to create a dynamic structure to store that information tab wise.
Below is the Map Structure that i wanted to created dynamically as JavaScript object.
To create this map structure, i had dynamically inserted the key in the object with the name of Tab and Features.
To insert keys dynamically inside the object use the Notation []. This will check if there is already a key in the object with the name passed. If there is already a key then it will pass the value to that key.Else it will introduce a new key with the name passed and assign values to it.
Code Snippet:
;
var aTabs = [ { tab: " pre >< console.log(map); }); i++; map[row.tab][row.featureName]="i;" } map[row.tab]="{};" undefined){ if(map[row.tab]="=" aTabs.forEach(function(row){ i="0;" var map="{};" ?feature1?}] featureName: ?tab2?, tab: { ?feature3?}, ?tab1?, ?feature2?}, { ?feature1?}, tab1?,>
// Sample array
ReplyDeletevar aTabs = [ { tab: "tab1", featureName: "feature1"},
{ tab: "tab1", featureName: "feature2"},
{ tab: "tab1", featureName: "feature3"},
{ tab: "tab2", featureName: "feature1"}]
var map = {};
var i = 0;
debugger;
aTabs.forEach(function(row){
debugger;
if(map[row.tab] == undefined){
map[row.tab] = {};
}
map[row.tab][row.featureName] = i;
i++;
});
console.log(map);