0

I am creating hybrid app, I am trying to create a object with string as index, its working fine in modern browsers but when i test my code in android 4.1 version it's throwing error Uncaught SyntaxError: Unexpected identifier, Scenario is, User can have 2-3 addresses, which i want to store with their title like "Home","Office","Other" etc, $scope.objaddr.title hold any one value home,office or other. how can i change this code to make it work in all versions ?

  var auth = {
  uid    : user.uid,
  token  : user.token,
  fname  : user.fname,
  lname  : user.lname,
  email  : user.email,
  mobile : user.mobile,
  addrs  : { [$scope.objaddr.title] : 
      {
        id     : $scope.objaddr.id,
        title  : $scope.objaddr.title,
        address: $scope.objaddr.address,
        pin    : $scope.objaddr.pin
      }
  }
};
$window.localStorage["auth"] = JSON.stringify(auth);
www.amitpatil.me
  • 3,001
  • 5
  • 43
  • 61
  • 5
    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) – Daedalus Jan 31 '16 at 09:22

1 Answers1

0
var addrs = {};

var auth = {
  uid    : user.uid,
  token  : user.token,
  fname  : user.fname,
  lname  : user.lname,
  email  : user.email,
  mobile : user.mobile,
  addrs   : addrs
 } 

  addrs[$scope.objaddr.title] =  
  {
    id     : $scope.objaddr.id,
    title  : $scope.objaddr.title,
    address: $scope.objaddr.address,
    pin    : $scope.objaddr.pin
  }
Beartums
  • 1,340
  • 1
  • 10
  • 15
  • 1
    Even though this answer worked, just dumping code isn't always the best idea; its always good to explain why your code works. – Daedalus Jan 31 '16 at 20:31