我有一个非常简单的JavaScript对象,我将其用作关联数组。是否有一个简单的函数允许我获取值的键,或者我必须迭代对象并手动找到它?
当前回答
我通常推荐lodash而不是underscore。
如果你有,就好好利用。
如果没有,那么应该考虑使用lodash。反转NPM包,它非常小。
下面是如何使用gulp测试它:
1)创建一个名为gulpfile.js的文件,包含以下内容:
// Filename: gulpfile.js
var gulp = require('gulp');
var invert = require('lodash.invert');
gulp.task('test-invert', function () {
var hash = {
foo: 1,
bar: 2
};
var val = 1;
var key = (invert(hash))[val]; // << Here's where we call invert!
console.log('key for val(' + val + '):', key);
});
2)安装lodash。倒转包装,大口吞咽
$ npm i --save lodash.invert && npm install gulp
3)测试它是否有效:
$ gulp test-invert
[17:17:23] Using gulpfile ~/dev/npm/lodash-invert/gulpfile.js
[17:17:23] Starting 'test-invert'...
key for val(1): foo
[17:17:23] Finished 'test-invert' after 511 μs
参考文献
https://www.npmjs.com/package/lodash.invert
https://lodash.com/
lodash和underscore的区别
https://github.com/gulpjs/gulp
其他回答
lodash way https://lodash.com/docs#findKey
Var用户= { 'barney':{'年龄':36,'活跃':true}, 'fred': {'age': 40, 'active': false}, '鹅卵石':{'年龄':1,'活动':true} }; _。findKey(users, {'age': 1, 'active': true}); //→'鹅卵石'
好像这个问题还没有被打得稀烂似的……
这里有一个,不管它给你带来了什么好奇心……
如果你确定你的对象将只有字符串值,你可能真的会用尽自己来想象这个实现:
var o = { a: '_A', b: '_B', c: '_C' }
, json = JSON.stringify(o)
, split = json.split('')
, nosj = split.reverse()
, o2 = nosj.join('');
var reversed = o2.replace(/[{}]+/g, function ($1) { return ({ '{':'}', '}':'{' })[$1]; })
, object = JSON.parse(reversed)
, value = '_B'
, eulav = value.split('').reverse().join('');
console.log('>>', object[eulav]);
也许这里有一些有用的东西……
希望这能让你开心。
我通常推荐lodash而不是underscore。
如果你有,就好好利用。
如果没有,那么应该考虑使用lodash。反转NPM包,它非常小。
下面是如何使用gulp测试它:
1)创建一个名为gulpfile.js的文件,包含以下内容:
// Filename: gulpfile.js
var gulp = require('gulp');
var invert = require('lodash.invert');
gulp.task('test-invert', function () {
var hash = {
foo: 1,
bar: 2
};
var val = 1;
var key = (invert(hash))[val]; // << Here's where we call invert!
console.log('key for val(' + val + '):', key);
});
2)安装lodash。倒转包装,大口吞咽
$ npm i --save lodash.invert && npm install gulp
3)测试它是否有效:
$ gulp test-invert
[17:17:23] Using gulpfile ~/dev/npm/lodash-invert/gulpfile.js
[17:17:23] Starting 'test-invert'...
key for val(1): foo
[17:17:23] Finished 'test-invert' after 511 μs
参考文献
https://www.npmjs.com/package/lodash.invert
https://lodash.com/
lodash和underscore的区别
https://github.com/gulpjs/gulp
我使用这个函数:
Object.prototype.getKey = function(value){
for(var key in this){
if(this[key] == value){
return key;
}
}
return null;
};
用法:
// 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
以下是我的解决方案:
例如,我假设我们有一个包含三个值对的对象:
function findKey(object, value) {
for (let key in object)
if (object[key] === value) return key;
return "key is not found";
}
const object = { id_1: "apple", id_2: "pear", id_3: "peach" };
console.log(findKey(object, "pear"));
//expected output: id_2
我们可以简单地编写一个findKey(数组,value),它接受两个参数,一个是对象,一个是你正在寻找的键的值。因此,该方法是可重用的,您不需要每次都手动迭代对象,只需为该函数传递两个参数。