0

I create an object like this:

[
  Object {
    Username = "James", Password = "12345", Email = "[email protected]"
  },
  Object {
    Username = "Auric", Password = "12345", Email = "[email protected]"
  }
]

What is the best way to retrieve a list of the property names? i.e. I would like to end up with some variable 'keys' such that:

Object = ["[email protected]", "[email protected]"]

Thanks.

1
  • [] Array. {} Object Commented Jan 20, 2015 at 9:52

2 Answers 2

1

You can use .map

var data = data.map(function (el) {
   return el.Email
})
Sign up to request clarification or add additional context in comments.

Comments

1

You have an array of objects there, so you'll need to loop through that then return the values you need.

    var myObjects = [
    {
    "Username" : "James", 
    "Password" : "12345", 
    "Email" : "[email protected]"
    },
  {
    "Username" : "Auric", 
    "Password" : "12345", 
    "Email" : "[email protected]"
  }
];


function getProps (key) {
    var values = [];
    myObjects.forEach(function (obj){
      values.push(obj[key]);
    });
    return values;
}

console.log(getProps('Email'));

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.