ari*_*rik 326 javascript object
我有一个非常简单的JavaScript对象,我将其用作关联数组.是否有一个简单的函数允许我获取值的键,或者我是否必须迭代对象并手动找到它?
小智 480
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
Run Code Online (Sandbox Code Playgroud)
ES6,没有原型突变或外部库.
jAn*_*ndy 173
没有可用的标准方法.您需要迭代,您可以创建一个简单的帮助器:
Object.prototype.getKeyByValue = function( value ) {
for( var prop in this ) {
if( this.hasOwnProperty( prop ) ) {
if( this[ prop ] === value )
return prop;
}
}
}
var test = {
key1: 42,
key2: 'foo'
};
test.getKeyByValue( 42 ); // returns 'key1'
Run Code Online (Sandbox Code Playgroud)
需要注意的一点是:即使上述工作正常,扩展任何主机或本机对象通常也是一个坏主意.prototype.我在这里做到了,因为它很适合这个问题.无论如何,你应该在这之外使用这个函数.prototype,并将对象传递给它.
ZER*_*ER0 109
如上所述,需要迭代.例如,在现代浏览器中,您可以:
var key = Object.keys(obj).filter(function(key) {return obj[key] === value})[0];
Run Code Online (Sandbox Code Playgroud)
哪里value包含您正在寻找的价值.说,我可能会使用一个循环.
否则你可以使用一个合适的"hashmap"对象 - 在JS中有几个实现 - 或者由你自己实现.
更新2018年
六年过去了,但我仍然在这里得到一些投票,所以我觉得更现代的解决方案 - 对于现代浏览器/环境 - 应该在答案中提及而不仅仅是在评论中:
const key = Object.keys(obj).find(key => obj[key] === value);
Run Code Online (Sandbox Code Playgroud)
当然它也可以是一个功能:
const getKeyByValue = (obj, value) =>
Object.keys(obj).find(key => obj[key] === value);
Run Code Online (Sandbox Code Playgroud)
ban*_*yan 98
与Underscore.js图书馆:
var hash = {
foo: 1,
bar: 2
};
(_.invert(hash))[1]; // => 'foo'
Run Code Online (Sandbox Code Playgroud)
小智 40
lodash方式https://lodash.com/docs#findKey
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, { 'age': 1, 'active': true });
// ? 'pebbles'Run Code Online (Sandbox Code Playgroud)
Paw*_*wel 32
function extractKeyValue(obj, value) {
return Object.keys(obj)[Object.values(obj).indexOf(value)];
}
Run Code Online (Sandbox Code Playgroud)
用于闭包编译器以提取编译后未知的密钥名称
更性感的版本,但使用未来的Object.entries功能
function objectKeyByValue (obj, val) {
return Object.entries(obj).find(i => i[1] === val);
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*uer 21
我用这个函数:
Object.prototype.getKey = function(value){
for(var key in this){
if(this[key] == value){
return key;
}
}
return null;
};
Run Code Online (Sandbox Code Playgroud)
用法:
// ISO 639: 2-letter codes
var languageCodes = {
DA: 'Danish',
DE: 'German',
DZ: 'Bhutani',
EL: 'Greek',
EN: 'English',
EO: 'Esperanto',
ES: 'Spanish'
};
var key = languageCodes.getKey('Greek');
console.log(key); // EL
Run Code Online (Sandbox Code Playgroud)
Kar*_*eem 16
保持原型清洁.
function val2key(val,array){
for (var key in array) {
if(array[key] == val){
return key;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
例:
var map = {"first" : 1, "second" : 2};
var key = val2key(2,map); /*returns "second"*/
Run Code Online (Sandbox Code Playgroud)
Sys*_*ter 15
不可迭代的解决方案
主功能:
var keyByValue = function(value) {
var kArray = Object.keys(greetings); // Creating array of keys
var vArray = Object.values(greetings); // Creating array of values
var vIndex = vArray.indexOf(value); // Finding value index
return kArray[vIndex]; // Returning key by value index
}
Run Code Online (Sandbox Code Playgroud)
具有键和值的对象:
var greetings = {
english : "hello",
ukranian : "??????"
};
Run Code Online (Sandbox Code Playgroud)
测试:
keyByValue("??????");
// => "ukranian"
Run Code Online (Sandbox Code Playgroud)
cra*_*tin 14
如果你有(或想要)对underscore.js的依赖,你可以使用findKey函数.例如,
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'
Run Code Online (Sandbox Code Playgroud)
ES6
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'
Run Code Online (Sandbox Code Playgroud)
ES5
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'
Run Code Online (Sandbox Code Playgroud)
Sah*_*kar 14
这对我获取对象的键/值有用。
let obj = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
'key4': 'value4'
}
Object.keys(obj).map(function(k) {
console.log("key with value: " + k + " = " + obj[k])
})Run Code Online (Sandbox Code Playgroud)
我创建了bimap库(https://github.com/alethes/bimap),它实现了一个功能强大,灵活高效的JavaScript双向映射接口.它没有依赖关系,可以在服务器端(在Node.js中,您可以安装它npm install bimap)和浏览器(通过链接到lib/bimap.js)中使用.
基本操作非常简单:
var bimap = new BiMap;
bimap.push("k", "v");
bimap.key("k") // => "v"
bimap.val("v") // => "k"
bimap.push("UK", ["London", "Manchester"]);
bimap.key("UK"); // => ["London", "Manchester"]
bimap.val("London"); // => "UK"
bimap.val("Manchester"); // => "UK"
Run Code Online (Sandbox Code Playgroud)
在两个方向上检索键值映射同样快.引擎盖下没有昂贵的对象/数组遍历,因此无论数据大小如何,平均访问时间都保持不变.
没有看到以下内容:
const obj = {
id: 1,
name: 'Den'
};
function getKeyByValue(obj, value) {
return Object.entries(obj).find(([, name]) => value === name);
}
const [ key ] = getKeyByValue(obj, 'Den');
console.log(key)Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)
由于这些值是唯一的,因此应该可以将这些值添加为一组额外的键。这可以通过以下快捷方式完成。
var foo = {};
foo[foo.apple = "an apple"] = "apple";
foo[foo.pear = "a pear"] = "pear";
Run Code Online (Sandbox Code Playgroud)
这将允许通过键或值进行检索:
var key = "apple";
var value = "an apple";
console.log(foo[value]); // "apple"
console.log(foo[key]); // "an apple"
Run Code Online (Sandbox Code Playgroud)
这确实假设键和值之间没有公共元素。
鉴于input={"a":"x", "b":"y", "c":"x"}...
output={"x":"a","y":"b"}):input = {
"a": "x",
"b": "y",
"c": "x"
}
output = Object.keys(input).reduceRight(function(accum, key, i) {
accum[input[key]] = key;
return accum;
}, {})
console.log(output)Run Code Online (Sandbox Code Playgroud)
output={"x":"c","y":"b"}):input = {
"a": "x",
"b": "y",
"c": "x"
}
output = Object.keys(input).reduce(function(accum, key, i) {
accum[input[key]] = key;
return accum;
}, {})
console.log(output)Run Code Online (Sandbox Code Playgroud)
output={"x":["c","a"],"y":["b"]}):input = {
"a": "x",
"b": "y",
"c": "x"
}
output = Object.keys(input).reduceRight(function(accum, key, i) {
accum[input[key]] = (accum[input[key]] || []).concat(key);
return accum;
}, {})
console.log(output)Run Code Online (Sandbox Code Playgroud)
ES6 +一线
let key = Object.keys(obj).find(k=>obj[k]===value);
Run Code Online (Sandbox Code Playgroud)
要么
let key = Object.keys(obj)[Object.values(obj).indexOf(value)];
Run Code Online (Sandbox Code Playgroud)
要么
let key = ( Object.entries(obj).find(([k,v])=>v===value) || [] )[0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
433304 次 |
| 最近记录: |