6

When we have an object in JS like

var obj = {a: "apple", p: "pen"};

then we can destructure it as follows

 var {a, p} = obj; /* a = 'apple', p = 'pen' */

i want to know in case when keys are integers, how can we destructure it ? since we cannot declare integers as variable name

 var obj = {0: 'pineapple', 1: 'pen'};
Vikramaditya
  • 5,444
  • 6
  • 34
  • 45
  • you can use `keys = Object.keys(obj)` it will give the array of all the keys, then you can access any value by using the keys, like `a=obj[keys[index]]`, is this what you want ?? – Mayank Shukla Dec 15 '16 at 06:30
  • Is this an array-like object such as `arguments` or `NodeList`? Or is this an object where there are other string properties? If it's array-like, you can use array destructuring. – 4castle Dec 15 '16 at 06:31

1 Answers1

10

Just like any other assigning to new variable names

var {0:a, 1:b} = obj;
Artem Dudkin
  • 588
  • 3
  • 11