1

How to destructure this object:

const game = {
    title: "YS",
    developer: "Falcom",
    releases: {
      "Oath In Felghana": ["USA", "Japan"],
      "Ark Of Napishtim": {
        US: "20 USD",
        JAP: "10 USD",
      },
      Origin: "30 USD",
    },
  };

so I can get ("Oath In Felghana")the first key name of releases object in a variable. I tried this but it didn't work

 const{Object.keys(game.releases)[0]:keyName} = game

PS: I want to get the key Name (Oath In Felghana)

not the value ["USA", "Japan"]

B-M Amine
  • 39
  • 1
  • 6
  • If it was without spaces you could do it like this: `const { releases: { OathInFelghana } } = game` – deaponn Sep 01 '22 at 13:00
  • 1
    Does this answer your question? [How to destructure object properties with key names that are invalid variable names?](https://stackoverflow.com/questions/38762715/how-to-destructure-object-properties-with-key-names-that-are-invalid-variable-na) – amlxv Sep 01 '22 at 13:05
  • thank you but no I want to store the key name not the value – B-M Amine Sep 01 '22 at 13:06
  • `const [ game ] = Object.keys(game.releases);` – Peter Seliger Sep 01 '22 at 13:13
  • s there any other way to do it ? because I want to destructure all object properties in a single destructuring Assignment. – B-M Amine Sep 01 '22 at 13:19

3 Answers3

2

You can get the key name as follows:

const [ keyName ] = Object.keys(game.releases);
deaponn
  • 837
  • 2
  • 12
  • this will store the value ["USA", "Japan"] I want to store the key name "Oath In Felghana" – B-M Amine Sep 01 '22 at 13:08
  • thank you bro is there any other way to do it ? because I want to destructure all object properties in a single destructuring Assignment. – B-M Amine Sep 01 '22 at 13:15
2

From the above comments ...

const [ game ] = Object.keys(game.releases); – Peter Seliger

[I]s there any other way to do it ? because I want to destructure all object properties in a single destructuring Assignment. – B-M Amine

In case the OP wants to separate all game name related entries of game.releases from the non related Origin property, the OP needs to utilize the object destructuring rest property

const game = {
  title: "YS",
  developer: "Falcom",
  releases: {
    "Oath In Felghana": ["USA", "Japan"],
    "Ark Of Napishtim": {
      US: "20 USD",
      JAP: "10 USD",
    },
    Origin: "30 USD",
  },
};
const [ firstGameName ] = Object.keys(game.releases);

console.log({ firstGameName });

// OP ... [I]s there any other way to do it ?
//        because I want to destructure all
//        object properties in a single
//        destructuring Assignment.

const allGameNames = Object
  .keys(
    // rest property as of object destructuring ...
    // ... see [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#rest_property]
    (({ Origin, ...releaseEntries }) => releaseEntries)(game.releases)
  );
console.log({ allGameNames });
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
2

https://jsfiddle.net/c0ne2Lrv/8/

you will find the answer here.

const game = {
  title: "YS",
  developer: "Falcom",
  releases: {
    "Oath In Felghana": ["USA", "Japan"],
    "Ark Of Napishtim": {
      US: "20 USD",
      JAP: "10 USD",
    },
    Origin: "30 USD",
  },
};

({
  title: t,
  developer: d,
  releases:  {
    "Oath In Felghana":[u,j],
    "Ark Of Napishtim":{US:u_price, JAP:j_price},
    Origin:or
  }
} = game);

[o,a,] = Object.keys(game.releases);




// Write Your Destructuring Assignment/s Here

console.log(`My Favourite Games Style Is ${t} Style`);
// My Favourite Games Style Is YS Style

console.log(`And I Love ${d} Games`);
// And I Love Falcom Games

console.log(`My Best Release Is ${o} It Released in ${u} and ${j}`);
// My Best Release Is Oath In Felghana It Released in USA & Japan

console.log(`Although I Love ${a}`);
// Although I Love Ark Of Napishtim

console.log(`${a} Price in USA Is ${u_price}`);
// Ark Of Napishtim Price in USA Is 20 USD

console.log(`${a} Price in Japan Is ${j_price}`);
// Ark Of Napishtim Price in Japan Is 10 USD

console.log(`Origin Price Is ${or}`);
// Origin Price Is 30 USD