1

I'm using Object.keys() to fetch the object key while iterating the parent array as shown below. I was wondering if there are other ways to capture the object key if I know that there will be only one. arrOfObjs[i][keyArr[0]] gives me a headache.

var arrOfObjs = [
    {"key1":"val1"},
    {"key2":"val2"},
    {"key3":"val3"},    
]
for(var i in arrOfObjs){
    var keyArr = Object.keys(arrOfObjs[i]);
    console.log(keyArr[0], arrOfObjs[i][keyArr[0]]);
}
WouldBeNerd
  • 629
  • 11
  • 29
  • 1
    What is problem with `keyArr[0]`? – Tushar Feb 11 '16 at 11:34
  • *"I'm using Object.keys() to fetch the object key while iterating the parent array as shown below."* That's not what you're doing, no. The keys you're getting have nothing to do with the loop or the array. They relate to one entry *in* the array. – T.J. Crowder Feb 11 '16 at 11:37

3 Answers3

2

Preface: Don't use for-in to loop over arrays as in your example, at least not unless you're sure of what you're doing. More about looping arrays in this answer.

I was wondering if there are other ways to capture the object key if I know that there will be only one.

There's no shorter version of getting the only own enumerable property name from an object than Object.keys(obj)[0]. But you can do it earlier if the [0] is bothering you:

for(var i in arrOfObjs){ // See note above, don't use for-in here
    var keyArr = Object.keys(arrOfObjs[i])[0];
    // Note ------------------------------^^^
    console.log(keyArr, arrOfObjs[i][keyArr]);
}

You could do this:

for(var i in arrOfObjs){ // See note above, don't use for-in here
    for (var key in arrOfObjs[i]) {
        console.log(key, arrOfObjs[i][key]);
        break; // You've said you know there's only one, but...
    }
}

...but it doesn't buy you anything other than (I suppose) avoiding calling Object.keys, and in fact your Object.keys code filters out inherited properties, which may be useful (though it's not necessary in your example).

The objects in your example don't have any inherited properties (unless someone's done something insane with Object.prototype), but to be fully equal to your Object.keys example, we'd have to throw in another function call:

for(var i in arrOfObjs){ // See note above, don't use for-in here
    for (var key in arrOfObjs[i]) {
        if (arrOfObj[i].hasOwnProperty(key)) {
            console.log(key, arrOfObjs[i][key]);
            break; // You've said you know there's only one, but...
        }
    }
}

And in fact, to be completely the same, we'd have to go further:

for(var i in arrOfObjs){ // See note above, don't use for-in here
    for (var key in arrOfObjs[i]) {
        if (Object.prototype.hasOwnProperty.call(arrOfObj[i], key)) {
            console.log(key, arrOfObjs[i][key]);
            break; // You've said you know there's only one, but...
        }
    }
}
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

Well, I don't mind the [0], I would first remove the for loop. like:

var arrOfKeys = arrOfObjs.map((obj) => Object.keys(obj)[0])

licancabur
  • 635
  • 5
  • 11
-1

you can do that with a very clean way by using Underscore.js

var keys = _.map(arrOfObjs, function(v, k) { return k; });
aitnasser
  • 1,216
  • 1
  • 9
  • 23
  • `var keys = _.keys(arrOfObjs);` isn't remotely what the OP's code does. – T.J. Crowder Feb 11 '16 at 11:48
  • @T.J.Crowder I meant clean way – aitnasser Feb 11 '16 at 11:50
  • 1
    Well, as I said above, your code isn't remotely doing what the OP's code is doing. The equivalent would be `var firstKey = _.keys(arrOfObjs[i])[0];` vs. `var firstKey = Object.keys(arrOfObjs[i])[0];`. Adding a library doesn't make that cleaner. – T.J. Crowder Feb 11 '16 at 11:51
  • you can consult the official documentation of [here](http://underscorejs.org/#keys) – aitnasser Feb 11 '16 at 11:54
  • Which only confirms what I've said above: It does exactly what `Object.keys` does. (Which I knew. I wouldn't presume to comment if I didn't.) I think you need to re-read the question. – T.J. Crowder Feb 11 '16 at 11:55
  • 1
    @T.J.Crowder Oh, I'm sorry, you're absolutely right – aitnasser Feb 11 '16 at 12:02