0

Let us say we have an object such as

var city = {shortName : 'NY', fullName : 'New York'}:

Now I want to create a new object where the key should be the value of shortName from the above object and the Value of this Key should be the Value of fullName from above object.

The structure should be like this.

var abc = { NY : 'New York'};

I am aware of the destruction concept but I cannot figure out how to achieve this.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Faisal
  • 403
  • 4
  • 18
  • 1
    [Do not confuse JSON and JavaScript](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Quentin Feb 18 '19 at 09:54
  • 1
    This has nothing to do with destructuring (and even less, with destruction). Just create an object and assign a property as normal. – Bergi Feb 18 '19 at 09:58
  • Thanks @Quentin for you help. The link you provided is pretty good read and really helpful. To summarise JSON is just the string representation of structured data and in order to convert a JS object to a JSON representation we would use the Stringify Method. Correct me if I'm wrong. – Faisal Feb 18 '19 at 10:06
  • Thank you clarifying @Bergi. – Faisal Feb 18 '19 at 10:07

1 Answers1

1

Simple trick:

var city = { shortName: "NY", fullName: "New York" };

var obj = {};
obj[city["shortName"]] = city["fullName"];
console.log(obj);

{ NY: 'New York' }

Sagar Rana Magar
  • 550
  • 1
  • 3
  • 18