Javascript get object key name

How to get a key name for subsequent? For example, I want "button1" and "button2"?

var buttons = { button1: { text: 'Close', onclick: function(){ } }, button2: { text: 'Close2', onclick: function(){ } } } var i; for(i in buttons){ if(buttons.hasOwnProperty(i)){ alert(buttons[i].text); } } 

I tried using .push() , although this did not work.

+60
javascript
Apr 26 2018-12-12T00:
source share
7 answers

This may be better understood if you change the wording a bit:

 var buttons = { foo: 'bar', fiz: 'buz' }; for ( var property in buttons ) { console.log( property ); // Outputs: foo, fiz or fiz, foo } 

Note that you iterate over the properties of an object, using property as a reference to each of them during each subsequent cycle.

MSDN says the following for ( variable in [object | array ] ) :

Before each iteration of the loop, the variable is assigned the name of the next property of the object or the index of the next element of the array. Then you can use it in any of the statements inside the loop to refer to the property of an object or element of an array.

Also note that the order of the properties of the object is not constant and may change unlike the order of the index of the array. This may come in handy.

+75
Apr 26 2018-12-12T00:
source share

ECMAscript edition 5 also offers you the neat methods Object.keys() and Object.getOwnPropertyNames() .

So,

 Object.keys( buttons ); // ['button1', 'button2']; 
+45
Apr 26 2018-12-12T00:
source share

Change alert(buttons[i].text); on alert(i);

+7
Apr 26 2018-12-12T00:
source share

The variable i is your key name.

+4
Apr 26 2018-12-12T00:
source share

Assuming you have access to Prototype, this might work. I wrote this code for myself just a few minutes ago; I only need one key at a time, so this is not the time for large lists of key: value pairs or to highlight multiple key names.

 function key(int) { var j = -1; for(var i in this) { j++; if(j==int) { return i; } else { continue; } } } Object.prototype.key = key; 

It is numbered to work just like arrays to save headaches. In case of your code:

 buttons.key(0) // Should result in "button1" 
+3
Jul 10 '13 at 17:54
source share

Here is a simple example to help you get the key name of an object.

var obj ={parts:{costPart:1000, salesPart: 2000}}; console.log(Object.keys(obj));

the conclusion will be "parts";

+1
Feb 08 '19 at 8:14
source share

Updating ES6 ... although both the filter and the card may need to be configured.

Object.entries(theObj) returns a representation of the array [[key, value],] of an object that you can work with using the array methods Javascript, .each () ,. any () ,. forEach () ,. filter () ,. map () ,. reduce (), etc.

It saves a lot of work on iterating over parts of an Object.keys(theObj) or Object.values() object separately.

 const buttons = { button1: { text: 'Close', onclick: function(){ } }, button2: { text: 'OK', onclick: function(){ } }, button3: { text: 'Cancel', onclick: function(){ } } } list = Object.entries(buttons) .filter(([key, value]) => '${key}'[value] !== 'undefined' ) //has options .map(([key, value], idx) => '{${idx} {${key}: ${value}}}') console.log(list) 

0
Aug 19 '19 at 5:14
source share



All Articles