How to Get a Key in a JavaScript Object by Its Value

How to get a key in a JavaScript object by its value?

function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}

ES6, no prototype mutations or external libraries.

Example,

function getKeyByValue(object, value) {

return Object.keys(object).find(key => object[key] === value);

}

const map = {"first" : "1", "second" : "2"};

console.log(getKeyByValue(map,"2"));

Get key using value from an object in JavaScript?

As you already assumed you need to iterate over the object's attributes and check the value.

for(var key in c) {
if(c[key] === whatever) {
// do stuff with key
}
}

get key corresponding to value in js object

You can simply fetch all the keys using Object.keys() and then use .find() function to get the key out from that array, and then nicely wrap it in a function to make it modular.

var inspectionMapping = {

'a_pillar_lh': "A Pillar LH",

'b_pillar_lh': "B Pillar LH"

};

Object.prototype.getKey = function(value) {

var object = this;

return Object.keys(object).find(key => object[key] === value);

};

alert(inspectionMapping.getKey("A Pillar LH"));

How to get the key of a key/value JavaScript object

If you want to get all keys, ECMAScript 5 introduced Object.keys. This is only supported by newer browsers but the MDC documentation provides an alternative implementation (which also uses for...in btw):

if(!Object.keys) Object.keys = function(o){
if (o !== Object(o))
throw new TypeError('Object.keys called on non-object');
var ret=[],p;
for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
return ret;
}

Of course if you want both, key and value, then for...in is the only reasonable solution.

Get the key of a JS Object from the value

You can achieve this with a single line of code by using 3 JavaScript methods - Object.keys(), Array.find() & Array.indexOf() :

const exampleObject = {
key1: ['Geeks','test1','test2'],
key2: ['Javascript','test3','test4']
};

function getKeyByValue(object, value) {
const res = Object.keys(exampleObject).find(key => exampleObject[key].indexOf(value) !== -1);
return res || ''
}

console.log(getKeyByValue(exampleObject,'Geeks'))

Find key for specific value on object in JS

To find the key use a predicate function with _.findKey():

var locs = {'Aberdeen': 304, 'Aberystwith': 109, 'Belfast': 219, 'Birmingham': 24, 'Brighton': 147 };

var key = _.findKey(locs, function(v) {

return v === 304;

});

console.log(key);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Get a key in a JavaScript object by its value and rename it

A little bit tricky as alghoritm but it works:

let data = [{key1:"Jhon", keyDate1:"08/13/2021",key2:"Eric", keyDate2:"01/01/2021",key3:"Jack", keyDate3:"10" }];

let i = 1;
let result = [];
let explored = [];
data.forEach(x => {
let resultObj = {};
for (const [key, val] of Object.entries(x)) {
let newKey = key;
let newKeyDate = null;
if (val === "Jack") {
newKey = "conditionMet" + i;
newKeyDate = "conditionDateMet" + i;
}
if (!explored.includes(key)) resultObj[newKey] = val;
if (newKeyDate) {
resultObj[newKeyDate] = x["keyDate" + i];
explored.push("keyDate" + i)
}
if(!key.includes("Date")) i++;
}
result.push(resultObj)
})
console.log(result)

I need to get the key that has the value 0

You can make use of Object.keys and Array.find to get the single key from the object.

var json = {
"productSkuInventoryStatus": {
"005340304004": 0,
"005340304003": 0,
"005340304002": 0,
"005340304001": 0,
"005340302401": 0,
"005340302402": 0,
"005340301401": 0,
"005340304005": 0,
"005340301403": 0,
"005340302405": 0,
"005340301402": 0,
"005340301405": 0,
"005340302403": 1,
"005340301404": 0,
"005340302404": 0
}
}

const result = Object.keys(json.productSkuInventoryStatus).find(key => json.productSkuInventoryStatus[key] > 0);
console.log(result)


Related Topics



Leave a reply



Submit