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"));
Answer from UncleLaz on Stack Overflowfunction 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"));
No standard method available. You need to iterate and you can create a simple helper:
Object.prototype.getKeyByValue = function( value ) {
for( var prop in this ) {
if( this.hasOwnProperty( prop ) ) {
if( this[ prop ] === value )
return prop;
}
}
}
var test = {
key1: 42,
key2: 'foo'
};
test.getKeyByValue( 42 ); // returns 'key1'
One word of caution: Even if the above works, its generally a bad idea to extend any host or native object's .prototype. I did it here because it fits the issue very well. Anyway, you should probably use this function outside the .prototype and pass the object into it instead.
javascript - Find object by key in array of object - Stack Overflow
jquery - Find item by key in Javascript - Stack Overflow
Search the DOM for an object's key value and return the name of the object
javascript - Find element in Object Array by key value - Stack Overflow
Videos
The value you return in the reduce() callback is used as the accumulator in the next iteration. Since you don't return anything when the key doesn't match raceId, your code will only work if the matching key is the last element of the array.
Use find() rather than reduce(), and then get the property from that.
const found = raceScoringConfigGroups.find(el => el[raceId]);
const reduced = found?.[raceId];
Can you try this?
const result = array.filter(el => Object.keys(el) === raceId)[0];
You should change that to objects
var meh={"cars" :27 , "bikes" :85, "skates" :4};
Now you can simply access it via keys
alert(meh['cars']); //27
If you have access to the code and can change the object, change it to something like this:
meh = {
'cars': 27,
'bikes': 85,
'skates': 4
};
and you can access them with keys like
meh["cars"] //will give you 27
If you cannot change the code, then the only way I see is using jQuery.each and comparing each key with your known key and assigning it to a temp variable.
I have been playing around with this for a bit and can't figure it out.
I want to query all of the objects in the DOM, looking through the sets, and when one of the objects of the set in question has a matching key value, I want to return the key value.
window.mysteryObj = {
obj1:{
abc: 1,
def: 2,
},
obj2:{
ghi: 3,
jkl: 4,
}
}
const keyValue = "ghi"
function findMysteryObj (key){
//does some magic
return objectName
}
findMysteryObj(keyValue); // should return "mysteryObj"Is this possible? If someone could put me on the right track that would be amazing!
I thought about restructuring and having object instead of array
Yes, that's fine.
BUT what if we want to keep the order?
I tend to use an extra array that contains the keys in the correct order, like
var order = [17, 34, 47];
To loop them, you'd use
for (var i=0; i<order.length; i++) {
… someObject[order[i]] …
}
You should prefix the id to avoid naming collisions, like this:
var database =
{
'row_17' : {id:17, color:'black', width:50},
'row_34' : {id:34, color:'red', width:150},
'row_49' : {id:49, color:'gree', width:10}
};
At this point you can query the object with this code:
function retrieve(database, id)
{
id = 'row_' + id;
if (!database.hasOwnProperty(id))
{
return null;
}
return database[id];
}
The persist function would be:
function persist(database, obj)
{
database['row_' + obj['id']] = obj;
}
If you need to keep an order you have first to understand what order you are talking about.
Of the id? Of the insert? Or of an arbitrary property?
There are solutions to all of these by filtering (extracting the items and putting them into a separate array that would be the query result or with additional fields or structures.
EDIT: how to keep insertion order
You'll need an array that keeps track of order:
database._ordered = [];
On insert, push the item there, too:
database._ordered.push(obj);
Now you can pick single items by key and all items ordered. There is no way you can have an order in an object, it's simply the wrong tool for that.
The row_ prefix is recommended to avoid naming collisions with methods and whatnot. An object should hold properties and methods, if you use it like a dictionary at least prevent interference by prefixing.