如何获取JavaScript对象的第一个键名?

Let's say we have an array of fruits,

假设我们有很多水果,

const fruits = ['apple', 'banana', 'orange', 'peach']
console.log(fruits[0]);

Output

输出量

"apple"

The first member of our fruits array is apple and we can easily access it since we know the first element is on the 0th index of the array. Let's say we now have an object whose first key name is fruits and the value is an array of fruits.

我们的Fruits数组的第一个成员是apple,由于我们知道第一个元素在数组的 0 索引上,因此我们可以轻松地访问它。 假设我们现在有一个对象,其第一个键名称是fruits,值是fruits的数组。

const fruitObj={
	fruits: ['apple','banana','orange','peach']
}
console.log(fruitObj[0]);

Output

输出量

undefined

Since objects do not have indices, we cannot call for the first property, second or last one the way we do in arrays. Then how do we do it?

由于对象没有索引,因此我们无法像在数组中那样调用第一个属性,第二个或最后一个属性。 那我们该怎么做呢?

The easiest way to do this would be to iterate over the object using the for loop and break out of the loop as soon as we get through the first iteration. We'll store the key in a variable whose scope will not be contained inside the loop so we can simply output that variable onto the console and we'll get the first key name of our object.

最简单的方法是使用for循环遍历对象,并在第一次迭代后立即退出循环。 我们将密钥存储在一个变量中,该变量的作用域不会包含在循环中,因此我们可以简单地将该变量输出到控制台上,并获得对象的第一个密钥名称。

Let's create a simple object first,

首先创建一个简单的对象,

const fruitObj={
	fruits: ['apple','banana','orange','peach']
}
console.log(fruitObj[0]);

Output

输出量

{type: "Monster", health: 10000, planet: "Jupiter", color: "Grey"}

We need to get type somehow. Let's do it the way we just discussed,

我们需要以某种方式获取类型 。 让我们按照我们刚才讨论的方式来做,

var key;
for (prop in mojo) {
    key = prop;
    break;
}
console.log(key);

Output

输出量

type

Instead of breaking the loop we can create a function to do so and simply return that key from the function.

除了创建循环,我们还可以创建一个函数来执行此操作,只需从函数中返回该键即可。

function getFirstKey(obj) {
    for (key in obj) {
        return key;
    }
}
console.log(getFirstKey(mojo));

Output

输出量

"type"

This is cool but it seems a little off. Running a loop and breaking it, we're only being witty. Can we do a little better?

这很酷,但似乎有点不对劲。 运行一个循环并打破它,我们只是机智。 我们可以做得更好吗?

Remember in the beginning how I said and even proved that since objects do not have index referencing we cannot simply call for the first element or last element etc. However, if we somehow get our object to behave like an array, we can easily do so. But the conversion of an object into an array seems like a task. But we don't need to do that. All we have to do is get all the keys in an array and then we can simply get the first key by referencing the 0th index in that array. We can use the object.keys() method,

请记住,在一开始时我曾说过什么,甚至证明了由于对象没有索引引用,所以我们不能简单地调用第一个元素或最后一个元素等。但是,如果我们以某种方式使对象表现得像数组,我们可以轻松做到这一点。 。 但是将对象转换为数组似乎是一项任务。 但是我们不需要这样做。 我们要做的就是获取数组中的所有键,然后我们可以简单地通过引用该数组中的第0个索引来获取第一个键。 我们可以使用object.keys()方法

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

Output

输出量

(4) ["type", "health", "planet", "color"]

As you can see, the object.keys() returns us an array. Now all we have to do is reference the 0th index of this array,

如您所见, object.keys()返回一个数组。 现在,我们要做的就是引用此数组的 0 索引,

console.log(Object.keys(mojo)[0]);

Output

输出量

type

Cool. A one-liner to get the first key name of a JavaScript object!

凉。 一次性获取JavaScript对象的键名!

翻译自: https://www.includehelp.com/code-snippets/how-to-get-the-first-key-name-of-a-javascript-object.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值