1

I'm trying to convert the following javascript object to a Dart map:

var users = { 
    1 : {
      first_name: 'James',
      last_name: 'Smith',
      email: '[email protected]',
    },
    2 : {
      first_name: 'Robin',
      last_name: 'Doe',
      email: '[email protected]',
    }

I've tried:

var users = { 
    "1" : {
      first_name: 'James',
      last_name: 'Smith',
      email: '[email protected]',
    },
    "2" : {
      first_name: 'Robin',
      last_name: 'Doe',
      email: '[email protected]',
    } 

but I'm unable to use it as a map with the numbers in quotes or without(throws errors).

var keys = users.getKeys(); //NoSuchMethodError : method not found: 'getKeys'
assert(keys.length == 2);
assert(new Set.from(keys).contains('2'));

1 Answer 1

5

Use (single or double) quotes for attribute names:

var users = {
   "1" : {
     "first_name": "James",
     "last_name": "Smith",
     "email": "[email protected]",
   },
   "2" : {
     "first_name": "Robin",
     "last_name": "Doe",
     "email": "[email protected]"
   }
 };

Also, getKeys method does not exists, use keys instead:

 var keys = users.keys;
 assert(keys.length == 2);
 assert(keys.contains("2"));
 assert(users["1"]["first_name"] == "James");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.