Getting the key value of object


#1

I have custom.js inside config/ folder

"use strict";

module.exports = {
	status: {
		pending: 0,
		processed: 1,
		success: 2,
		canceled: 3
	}
};

I can access the value by doing : Config.get(‘custom.status.pending’) //return 0

But how can I get the key itself? Searching “0” and expecting “pending” as the result

Thanks


#2

This is not really an Adonis question, but I’ll help you anyways :slight_smile:

const flippedStatus  = Object
  .keys(Config.get('custom.status'))
  .reduce(function(obj, key) {
     obj[ data[key] ] = key;
     return obj;
  }, {});

You can also do this inside the custom.js once, and have it accessible across the app. Just don’t use Config.get there, but store the status in a variable at the top of the file and then manipulate it as needed.


#3

Your code returned : data is not defined

I had found the way to do this btw, thanks a lot for your help!