2

Supposing I have such object:

var obj = {
  'a': 'fizzle',
  'b': 'wizzle',
  'c': 'bum',
  'd': 'crum'
}

I would like to take the key names and flatten these into an array, like so:

// -> ['a', 'b', 'c', 'd'];

I could achieve this with a simple object loop, however I am wondering if there is a common underscore utility that could turn it into a one-liner. I looked through the underscore functions and couldn't find one for this.

1 Answer 1

2

You could just use Object.keys() method available on the native Object constructor , Which outputs the original objects own enumerable properties.

Object.keys(obj);
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, 7 years of web dev and somehow I've never seen that before. Thanks a bunch.
@dc2 . Sure no worries.. It happens with everyone once in a while :)

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.