简体   繁体   English

如何在数组中捕获对象键

[英]how to catch object key inside array

I'm using Object.keys() to fetch the object key while iterating the parent array as shown below. 我正在使用Object.keys()在迭代父数组时获取对象键,如下所示。 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. arrOfObjs[i][keyArr[0]]让我头疼。

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]]);
}

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. 前言:请勿像示例中那样使用for-in遍历数组,至少除非您确定自己在做什么,否则至少不要这样做。 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] . 从对象获取唯一的可枚举属性名称的最简单方法莫过于Object.keys(obj)[0] But you can do it earlier if the [0] is bothering you: 但是如果[0]困扰您,您可以更早地进行操作:

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). ...但是,除了避免我调用Object.keys (我想)之外,它什么都买不到,实际上,您的Object.keys代码可以过滤掉继承的属性,这可能很有用(尽管在您的示例中没有必要)。

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: 您的示例中的对象没有任何继承的属性(除非有人用Object.prototype疯狂地完成了某些事情),但是要完全等于您的Object.keys示例,我们必须抛出另一个函数调用:

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...
        }
    }
}

Well, I don't mind the [0] , I would first remove the for loop. 好吧,我不介意[0] ,我会先删除for循环。 like: 喜欢:

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

您可以使用Underscore.js以一种非常干净的方式进行操作

var keys = _.map(arrOfObjs, function(v, k) { return k; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM