0

Can I create an Array from an Object just in one line? I don't want all the values object, just a selection:

const myObject = { a: 'foo', b: 'bar', c:'yep' }

const { a, c } = myObject
const myArray = Array.of(a, c)

console.log(myArray)

Could I use destructuring in some way inside the Array.of parameter?

4
  • Why dont you want to use Object.keys & Array.map methods? Commented Oct 27, 2016 at 9:44
  • I only asked myself how to keep it as simple as possible :) Commented Oct 27, 2016 at 10:22
  • If your point is to codegolf the solution, then: myArray=(({a,c})=>[a,c])(myObject) Commented Oct 27, 2016 at 11:08
  • Just write your solution in one line. A bit shorter: const { a, c } = myObject, myArray = [a, c]. Even an imaginary solution cannot be shorter, so maybe you explain your objective. Commented Oct 27, 2016 at 11:26

1 Answer 1

1

Why not just :

const myObject = { a: 'foo', b: 'bar', c:'yep' };
let arr = Array.of(myObject.a, myObject.c);
console.log(arr);
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that could be a reasonable solution but I just wanted to avoid repeat references to the Object or to the keys

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.