Javascript get object key name
This might be better understood if you modified the wording up a bit:
var buttons = {
foo: 'bar',
fiz: 'buz'
};
for ( var property in buttons ) {
console.log( property ); // Outputs: foo, fiz or fiz, foo
}
Note here that you're iterating over the properties of the object, using property
as a reference to each during each subsequent cycle.MSDN says of for ( variable in [object | array ] )
the following:
Note also that the property order of an object is not constant, and can change, unlike the index order of an array. That might come in handy.Before each iteration of a loop, variable is assigned the next property name of object or the next element index of array. You can then use it in any of the statements inside the loop to reference the property of object or the element of array.
Getting the object's property name
Use Object.keys():
var myObject = { a: 'c', b: 'a', c: 'b' };var keyNames = Object.keys(myObject);console.log(keyNames); // Outputs ["a","b","c"]
Get the key names of the object with value equals to something
ES6+ in one line
let key = Object.keys(obj).find(k=> obj[k] === value);
Get all the keys withlet keys = Object.keys(obj).filter( k => obj[k] === 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"));
Javascript get Object property Name
If you know for sure that there's always going to be exactly one key in the object, then you can use Object.keys
:
theTypeIs = Object.keys(myVar)[0];
JS get key name using map()
const obj = {
"thumbnail": {
"height": 150,
"width": 150,
},
"medium": {
"height": 165,
"width": 300,
},
"large": {
"height": 352,
"width": 640,
}
}
const retrnArr = Object.keys(obj).map(e =>{
return { 'value': e , 'label' : e.toUpperCase()}
});
console.log(retrnArr)
Get key name of a javascript object in an array of objects
The documentation for map says:
Creates an array of values by running each element in collection thruAs you can see, the second parameter passed to the iteratee is the original key in the collection. So, just add another parameter to the iteratee and use that:
iteratee. The iteratee is invoked with three arguments: (value,
index|key, collection).
const categoryValues = _.map(groups, (element, name) => {
return {
categoryName: name,
categoryValue: _.reduce(element,(acc, el) => el.value,0),
};
});
Get the first key name of a JavaScript object
In Javascript you can do the following:
Object.keys(ahash)[0];
JavaScript set object key by variable
You need to make the object first, then use []
to set it.
var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
UPDATE 2021:
Computed property names feature was introduced in ECMAScript 2015 (ES6) that allows you to dynamically compute the names of the object properties in JavaScript object literal notation.
const yourKeyVariable = "happyCount";
const someValueArray= [...];
const obj = {
[yourKeyVariable]: someValueArray,
}
How to get the property name of object and use that property name in javascript
The only error in your code is mapped_errors.{obj_key}.msg
. You should be using []
to access the object's property value when you are using a variable to get the key name, as mapped_errors[obj_key]
.
var mapped_errors = { _id: { location: 'body', param: '_id', value: 123, msg: '_id is required and must be a string' } };
let obj_key = Object.keys(mapped_errors);console.log(mapped_errors[obj_key].msg);
mapped_errors = { _name: { location: 'body', param: '_name', value: 123, msg: '_name is required and must be a string' } } obj_key = Object.keys(mapped_errors);console.log(mapped_errors[obj_key].msg);
Related Topics
React Jsx: Selecting "Selected" on Selected <Select> Option
Why Is 'This' Undefined Inside Class Method When Using Promises
Remove HTML Comments with Regex, in JavaScript
How to Use Getelementsbyclassname in JavaScript-Function
Subject VS Behaviorsubject VS Replaysubject in Angular
Node.Js Piping the Same Readable Stream into Multiple (Writable) Targets
How to Convert a Firestore Date/Timestamp to a Js Date()
What Do Double Brackets Mean in JavaScript and How to Access Them
Getting Access Is Denied Error on IE8
Initializing Select with Angularjs and Ng-Repeat
Understanding JavaScript Promise Object
Why Does JavaScript Getyear() Return 108
Problems with Circular Dependency and Oop in Angularjs
How to Get the Focused Element with Jquery
JavaScript Switch VS. If...Else If...Else
Properties of JavaScript Function Objects
How to Stop Intense JavaScript Loop from Freezing the Browser