1

I am working on an ionic 1 app, I do not know if there is a way but for example, here, is there a way I can dynamically assign a parent in this object depending on the value of the Variable?

var contact = {
     "Name": "John Doe",
     "available": true,
      Variable: [ 
       {
           "Location": "Home",
           "Number": "33"
       },
       {
           "Location": "Work",
           "Number": "22"
       }
     ]
};

Lets say Variable = "friends" Then

var contact = {
     "Name": "John Doe",
     "available": true,
     "Friends": [ 
       {
           "Location": "Home",
           "Number": "33"
       },
       {
           "Location": "Work",
           "Number": "22"
       }
     ]
};

I know I can use ES6, the Computed Property Names [Variable] but they are not working on older devices. Is there any alternative method?

Missak Boyajian
  • 1,965
  • 7
  • 32
  • 59

1 Answers1

1

Just plain old

var contact = {
     "Name": "John Doe",
     "available": true,
};
contact[Variable] = [ 
   {
       "Location": "Home",
       "Number": "33"
   },
   {
       "Location": "Work",
       "Number": "22"
   }
 ]
Artem Dudkin
  • 588
  • 3
  • 11