0

i have added a fiddle through that you can get the problem http://jsfiddle.net/ro59fxow/

var myFolders = ['abc','bcd','cda']; //these values are dynamic

var a1 = myFolders[0];
var folder = {
                a1:['11'],   //this is giving a1 ,i want here abc
                folder1: ['sss','www'],
                folder2 : ['uvw','xyz']
            }

myFolders.push(folder);
console.warn(myFolders);

thanks in advance

AKG
  • 221
  • 1
  • 3
  • 9
  • 2
    If you want `abc` there use `a1:a1`. Where first `a1` is key not variable. Your question seems unclear to me. – Manwal Oct 13 '15 at 11:20
  • Use bracket notation to access the member of an object if the key is stored in a variable `folder[a1] = ["11"];` - [fiddle](http://jsfiddle.net/w74L2x1r/) – Andreas Oct 13 '15 at 11:25
  • hi manwal thanks for reply ..i want dynamic key ,a1 =myfolders[0] means a1 is abc but this code is adding a1 place of abc .is there any way to add abc . abc is dynamic can be anything – AKG Oct 13 '15 at 11:26
  • Possible duplicate of [Using a variable for a key in a JavaScript object literal](http://stackoverflow.com/questions/2274242/using-a-variable-for-a-key-in-a-javascript-object-literal) – Andreas Oct 13 '15 at 11:34

2 Answers2

0

Merge the second array into the first one is using following code.

  Array.prototype.push.apply(myFolders,folder.folder2);

output:["abc", "bcd", "cda", "uvw", "xyz"]

Yasemin çidem
  • 623
  • 8
  • 17
0

You can use the below code should work from what I understood your question.

    var myFolders = ['abc','bcd','cda']; //these values are dynamic

    var a1 = myFolders[0];
    console.log(a1);
    var folder = {};
    folder[a1] = ['111']; //use this syntax to evaluate the variable as a key value.

    myFolders.push(folder);
    console.warn(myFolders);
Sagar Parikh
  • 306
  • 3
  • 7