如何通过JavaScript中的值获取对象中的键?

In this article, we'll see two methods on how to get a key in an object from its value? Usually, when we traverse through an object we traverse through its keys and can easily get the corresponding value. However, the reverse is actually a bit different. Consider the following object,

在本文中,我们将看到两种有关如何从对象的值中获取键的方法 通常,当我们遍历对象时,我们遍历对象的键并可以轻松获得相应的值。 但是,实际上相反。 考虑以下对象,

const car= {
	company: 'Merc', 
	model: 'Benz', 
	type: 'luxury sedan', 
	year: 2007, 
	modelNo: 'MB2007-4762', 
	topSpeed: 240
}

console.log(car);

Output

输出量

{company: "Merc", model: "Benz", type: "luxury sedan", year: 2007, modelNo: "MB2007-4762", …}
company: "Merc"
model: "Benz"
modelNo: "MB2007-4762"
topSpeed: 240
type: "luxury sedan"
year: 2007
__proto__: Object

We can easily get the value corresponding to a certain key using the dot notation or the index method. However, to get a key from value one way would be to iterate through its properties and check for a match for a value. We can do this using a for loop and hasOwnProperty() method.

我们可以使用点符号或索引方法轻松获得与某个键对应的值。 但是,从值中获取键的一种方法是迭代其属性并检查值是否匹配。 我们可以使用for循环和hasOwnProperty()方法来实现

function getkeyFromvalue(object, value) {
    for (let ob in object) {
        if (object.hasOwnProperty(ob)) {
            if (object[ob] === value)
                return ob;
        }
    }
}

console.log(getkeyFromvalue(car, 2007));

Output

输出量

"Year"

The reason we use the hasOwnProperty(), first is to check if the object had the same property or if it inherited that property from somewhere. Let's try this a few more times,

我们使用hasOwnProperty()的原因是,首先是要检查对象是否具有相同的属性,或者是否从某个地方继承了该属性。 让我们再尝试几次

console.log(getkeyFromvalue(car, 'Merc'));
console.log(getkeyFromvalue(car, 240));

Output

输出量

"company"
"topSpeed"

This way we can get a key in an object from its value. However, this is a naive approach where we check for every key through the object to match its value. We can use the find() method to compare keys to have a certain value that we want to find. We'll call this on an array of keys that we can get using the Object.keys() method that returns us all the keys of an object. Let's try to do it this way,

这样,我们可以从对象的值中获取键 。 但是,这是一种幼稚的方法,我们检查对象中的每个键以匹配其值。 我们可以使用find()方法比较键以具有要查找的特定值。 我们将在可以使用Object.keys()方法获取的键数组上调用此方法 ,该方法将向我们返回对象的所有键。 让我们尝试这样做

function getkeyFromvalue2(object, value) {
    return Object.keys(object).find(key => object[key] === value)
}

console.log(getkeyFromvalue2(car, 2007));
console.log(getkeyFromvalue2(car, 240));
console.log(getkeyFromvalue2(car, 'Benz'));

Output

输出量

"year"
"topSpeed"
"Model"

This method is much easier and also simpler with fewer lines of code. If we look into this, it's something very similar to how we did it before using the for loop. Object.keys() was introduced in ES15 and makes the task of storing all the keys somewhere and then iterating through them simpler.

这种方法更容易,代码行也更少。 如果我们仔细研究,这与使用for循环之前的操作非常相似。 ES.15中引入了Object.keys() ,使将所有密钥存储在某个位置,然后对其进行迭代的任务变得更加简单。

翻译自: https://www.includehelp.com/code-snippets/how-to-get-a-key-in-an-object-by-its-value-in-javascript.aspx

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值