1

I have the following object:

var text = { 'one' : 1, 'two' : 2, 'three' : 3};

I want to write the key 'one' with console.log(). How can I do it?

JJJ
  • 31,545
  • 20
  • 84
  • 99
  • That is not an array, it's an object. Also note that objects are unordered so you can't reliably take the "first" key (if that's what you want) because internally elements can be stored in any order. – JJJ May 15 '15 at 08:08
  • Do you need `'one'` or `1`? text.one => 1 or text['one'] => 1 – alain May 15 '15 at 08:14

3 Answers3

0

try:

var text = { 'one' : 1, 'two' : 2, 'three' : 3};
for (var i in text){
    console.log(i);
}
Yangguang
  • 1,765
  • 7
  • 10
0

You need to use the keys() function, as epoch pointed out in the comment section. For example :

console.log(Object.keys(text)); 

Can print one two three; If you want to be certain about your order, you'll have to use an array instead of an object, as properties in objects are not ordered.

Be careful, in older browers you'll have to define your own fucntion of keys, as explained in the doc i linked above, like so :

if (!Object.keys) {
  Object.keys = (function() {
    'use strict';
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length;

    return function(obj) {
      if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
        throw new TypeError('Object.keys called on non-object');
      }

      var result = [], prop, i;

      for (prop in obj) {
        if (hasOwnProperty.call(obj, prop)) {
          result.push(prop);
        }
      }

      if (hasDontEnumBug) {
        for (i = 0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) {
            result.push(dontEnums[i]);
          }
        }
      }
      return result;
    };
  }());
}
Community
  • 1
  • 1
Mekap
  • 2,045
  • 12
  • 26
0

Your question is kind of vague.

What exactly is it that you are trying to achieve? Do you just want to log the first prop in your obj?

If so, that would not be a good idea because the order of properties in an object is not guaranteed. The order might even change over time, depending on what happens to the object.

You could in fact use the Object.keys() method, which would do a for in on all enumerable properties of the object, but this would give you all properties and since you asked for a specific property to be logged to the console, this leads me to the conclusion that you probably are more interested in how to check for the existence of a property.

To make a long story short, if you really want to log a specific property you could just do

(text.hasOwnProperty('one')) ? console.log('one') : console.log('"one" does not exists');
DavidDomain
  • 13,592
  • 4
  • 36
  • 48