Home > Back-end >  How to get the key after using Object.enteries on a key value pair in JavaScript
How to get the key after using Object.enteries on a key value pair in JavaScript

Time:08-19

I have JSON as below, and I need it to be sorted according to value :

{
  '624d386f0f93e20d7bbf2654': 0.5586700439453125,
  '624d87b30f93e20d7bbf5e49': 0.5463325321674346,
  '624d89900f93e20d7bbf60d4': 0.37492166757583617,
  '624d8b0b0f93e20d7bbf63c0': 0.45542603731155396,
  '624d8bba0f93e20d7bbf65a1': 0.5411771833896637,
  '624d8cc60f93e20d7bbf691c': 0.3993324667215347,
  '624d8dab0f93e20d7bbf6ca4': 0.5388050019741059,
  '624d90340f93e20d7bbf77de': 0.458124116063118,
  '624d91cb0f93e20d7bbf8051': 0.5944154441356658,
  '624d9f93dc568b4a14f10c9b': 0.48740254938602445,
  '624da0e8dc568b4a14f10cab': 0.533133989572525,
  '624da29edc568b4a14f10cc5': 0.4393773108720779,
  '624da349dc568b4a14f10cd4': 0.5998590171337128,
  '624da670dc568b4a14f10ce6': 0.6130651950836181,
  '624da770dc568b4a14f10cfe': 0.6193588376045227,
  '624da808dc568b4a14f10d0b': 0.4232172936201095,
  '624da8b2dc568b4a14f10d1c': 0.44166545867919915,
  '624e72c60f93e20d7bbfbd2f': 0.960582548379898,
  '62553282501721a63bbddbd3': 0.7791991174221038,
  '62558525fcbec32d9e178a52': 0.8113727509975432
} 

If I use this function on the above data :

Object.entries(response.data).sort((a,b) => b[1] - a[1]);

where response.data consists of the above array object.

It sorts the JSON and gives the following result :

[
  [ '624e72c60f93e20d7bbfbd2f', 0.960582548379898 ],
  [ '62558525fcbec32d9e178a52', 0.8113727509975432 ],
  [ '62553282501721a63bbddbd3', 0.7791991174221038 ],
  [ '624da770dc568b4a14f10cfe', 0.6193588376045227 ],
  [ '624da670dc568b4a14f10ce6', 0.6130651950836181 ],
  [ '624da349dc568b4a14f10cd4', 0.5998590171337128 ],
  [ '624d91cb0f93e20d7bbf8051', 0.5944154441356658 ],
  [ '624d386f0f93e20d7bbf2654', 0.5586700439453125 ],
  [ '624d87b30f93e20d7bbf5e49', 0.5463325321674346 ],
  [ '624d8bba0f93e20d7bbf65a1', 0.5411771833896637 ],
  [ '624d8dab0f93e20d7bbf6ca4', 0.5388050019741059 ],
  [ '624da0e8dc568b4a14f10cab', 0.533133989572525 ],
  [ '624d9f93dc568b4a14f10c9b', 0.48740254938602445 ],
  [ '624d90340f93e20d7bbf77de', 0.458124116063118 ],
  [ '624d8b0b0f93e20d7bbf63c0', 0.45542603731155396 ],
  [ '624da8b2dc568b4a14f10d1c', 0.44166545867919915 ],
  [ '624da29edc568b4a14f10cc5', 0.4393773108720779 ],
  [ '624da808dc568b4a14f10d0b', 0.4232172936201095 ],
  [ '624d8cc60f93e20d7bbf691c', 0.3993324667215347 ],
  [ '624d89900f93e20d7bbf60d4', 0.37492166757583617 ]
]

now when I log Object.keys(return value) it gives this :

[
  '0',  '1',  '2',  '3',  '4',
  '5',  '6',  '7',  '8',  '9',
  '10', '11', '12', '13', '14',
  '15', '16', '17', '18', '19'
]

How can I get the keys now?

CodePudding user response:

If I see it correctly, the key/value pairs are now stored in an array (inside the original array). Assuming the data is stored in data you could just do:

data.foreach(el => console.log(el[0]))

To get an array with all the keys you could use the array reduce function.

data.reduce(el => el[0])

CodePudding user response:

You could use the Array.prototype.reduce() method or Array.prototype.map() method to transform your array of arrays into a single array of keys.

Array.prototype.reduce() example

const data = {
  '624d386f0f93e20d7bbf2654': 0.5586700439453125,
  '624d87b30f93e20d7bbf5e49': 0.5463325321674346,
  '624d89900f93e20d7bbf60d4': 0.37492166757583617,
  '624d8b0b0f93e20d7bbf63c0': 0.45542603731155396,
  '624d8bba0f93e20d7bbf65a1': 0.5411771833896637,
  '624d8cc60f93e20d7bbf691c': 0.3993324667215347,
  '624d8dab0f93e20d7bbf6ca4': 0.5388050019741059,
  '624d90340f93e20d7bbf77de': 0.458124116063118,
  '624d91cb0f93e20d7bbf8051': 0.5944154441356658,
  '624d9f93dc568b4a14f10c9b': 0.48740254938602445,
  '624da0e8dc568b4a14f10cab': 0.533133989572525,
  '624da29edc568b4a14f10cc5': 0.4393773108720779,
  '624da349dc568b4a14f10cd4': 0.5998590171337128,
  '624da670dc568b4a14f10ce6': 0.6130651950836181,
  '624da770dc568b4a14f10cfe': 0.6193588376045227,
  '624da808dc568b4a14f10d0b': 0.4232172936201095,
  '624da8b2dc568b4a14f10d1c': 0.44166545867919915,
  '624e72c60f93e20d7bbfbd2f': 0.960582548379898,
  '62553282501721a63bbddbd3': 0.7791991174221038,
  '62558525fcbec32d9e178a52': 0.8113727509975432
};

const sortedDataKeys = Object.entries(data)
  /**
   * Sort the data by value, resulting in an array of arrays;
   */
  .sort((a, b) => b[1] - a[1])
  /**
   * Use the reduce() method to transform the sorted array of arrays into a single array of keys.
   *
   * MDN documentation on Array.prototype.reduce()
   * ------------------------------------------------
   * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
   * ------------------------------------------------
   */
  .reduce((acc, [key]) => [...acc, key], []);

console.log(sortedDataKeys);

Array.prototype.map() example

const data = {
  '624d386f0f93e20d7bbf2654': 0.5586700439453125,
  '624d87b30f93e20d7bbf5e49': 0.5463325321674346,
  '624d89900f93e20d7bbf60d4': 0.37492166757583617,
  '624d8b0b0f93e20d7bbf63c0': 0.45542603731155396,
  '624d8bba0f93e20d7bbf65a1': 0.5411771833896637,
  '624d8cc60f93e20d7bbf691c': 0.3993324667215347,
  '624d8dab0f93e20d7bbf6ca4': 0.5388050019741059,
  '624d90340f93e20d7bbf77de': 0.458124116063118,
  '624d91cb0f93e20d7bbf8051': 0.5944154441356658,
  '624d9f93dc568b4a14f10c9b': 0.48740254938602445,
  '624da0e8dc568b4a14f10cab': 0.533133989572525,
  '624da29edc568b4a14f10cc5': 0.4393773108720779,
  '624da349dc568b4a14f10cd4': 0.5998590171337128,
  '624da670dc568b4a14f10ce6': 0.6130651950836181,
  '624da770dc568b4a14f10cfe': 0.6193588376045227,
  '624da808dc568b4a14f10d0b': 0.4232172936201095,
  '624da8b2dc568b4a14f10d1c': 0.44166545867919915,
  '624e72c60f93e20d7bbfbd2f': 0.960582548379898,
  '62553282501721a63bbddbd3': 0.7791991174221038,
  '62558525fcbec32d9e178a52': 0.8113727509975432
};

const sortedDataKeys = Object.entries(data)
  /**
   * Sort the data by value, resulting in an array of arrays;
   */
  .sort((a, b) => b[1] - a[1])
  /**
   * Use the map() method to transform the sorted array of arrays into a single array of keys.
   *
   * MDN documentation on Array.prototype.map()
   * ------------------------------------------------
   * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
   * ------------------------------------------------
   */
  .map([key]) => key);

console.log(sortedDataKeys);

Output

[
  '624e72c60f93e20d7bbfbd2f',
  '62558525fcbec32d9e178a52',
  '62553282501721a63bbddbd3',
  '624da770dc568b4a14f10cfe',
  '624da670dc568b4a14f10ce6',
  '624da349dc568b4a14f10cd4',
  '624d91cb0f93e20d7bbf8051',
  '624d386f0f93e20d7bbf2654',
  '624d87b30f93e20d7bbf5e49',
  '624d8bba0f93e20d7bbf65a1',
  '624d8dab0f93e20d7bbf6ca4',
  '624da0e8dc568b4a14f10cab',
  '624d9f93dc568b4a14f10c9b',
  '624d90340f93e20d7bbf77de',
  '624d8b0b0f93e20d7bbf63c0',
  '624da8b2dc568b4a14f10d1c',
  '624da29edc568b4a14f10cc5',
  '624da808dc568b4a14f10d0b',
  '624d8cc60f93e20d7bbf691c',
  '624d89900f93e20d7bbf60d4'
]
  • Related