[英]Return key of object whose value is an object that contains a property that matches a condition
I have an object我有一个对象
const CABLE_SOURCES = {
foxnews: {
id: "FOXNEWSW",
name: "Fox News",
},
cnn: {
id: "CNNW",
name: "CNN",
},
msnbc: {
id: "MSNBCW",
name: "MSNBC",
},
abc7: {
id: "KGO",
name: "ABC 7 news",
},
};
My current solution looks something like我目前的解决方案看起来像
function getKeyName(cableSourceId) {
let source = null;
for (let s in CABLE_SOURCES) {
if (CABLE_SOURCES[s].id === cableSourceId) {
source = s;
}
break;
}
return source;
}
getKeyName("FOXNEWSW")
// foxnews
Is there a more functional / elegant way to rewrite getKeyName
, either with ES6, or lodash?是否有更实用/更优雅的方式来重写
getKeyName
,无论是使用 ES6 还是 lodash?
One option is to .find
one of the object's entries whose value has that id
.一种选择是
.find
其值具有该id
的对象条目之一。 If such an entry is found, return the first item in it (the key), otherwise return null
:如果找到这样的条目,则返回其中的第一项(键),否则返回
null
:
const CABLE_SOURCES = { foxnews: { id: "FOXNEWSW", name: "Fox News", }, cnn: { id: "CNNW", name: "CNN", }, msnbc: { id: "MSNBCW", name: "MSNBC", }, abc7: { id: "KGO", name: "ABC 7 news", }, }; function getKeyName(cableSourceId) { const foundEntry = Object.entries(CABLE_SOURCES).find(([, { id }]) => id === cableSourceId); return foundEntry ? foundEntry[0] : null; } console.log(getKeyName("FOXNEWSW")); console.log(getKeyName("foo"));
I guess you can use Object.keys()
and Array.prototype.find()
combination.我猜你可以使用
Object.keys()
和Array.prototype.find()
组合。 Once you have the found object, just need to access name
property.一旦你找到了对象,只需要访问
name
属性。
Object.keys()
documentation states: Object.keys()
文档说明:
The Object.keys() method returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop.
Object.keys() 方法返回给定对象自己的可枚举属性名称的数组,其顺序与我们使用普通循环获得的顺序相同。
Array.prototype.find()
documentation states: Array.prototype.find()
文档说明:
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
find() 方法返回提供的数组中满足提供的测试函数的第一个元素的值。
Like the following:像下面这样:
const CABLE_SOURCES = { foxnews: { id: "FOXNEWSW", name: "Fox News", }, cnn: { id: "CNNW", name: "CNN", }, msnbc: { id: "MSNBCW", name: "MSNBC", }, abc7: { id: "KGO", name: "ABC 7 news", }, }; const getKeyName = text => CABLE_SOURCES[Object.keys(CABLE_SOURCES).find(i => CABLE_SOURCES[i].id === text)].name; const result = getKeyName("FOXNEWSW"); console.log(result);
I hope that helps!我希望这有帮助!
Lodash has a _.findKey()
method that accepts a predicate (object in this case), and returns the 1st key with a value that matches the predicate: Lodash 有一个
_.findKey()
方法,它接受一个谓词(在这种情况下是对象),并返回具有与谓词匹配的值的第一个键:
function getKeyName(id) { return _.findKey(CABLE_SOURCES, { id }); } const CABLE_SOURCES = {"foxnews":{"id":"FOXNEWSW","name":"Fox News"},"cnn":{"id":"CNNW","name":"CNN"},"msnbc":{"id":"MSNBCW","name":"MSNBC"},"abc7":{"id":"KGO","name":"ABC 7 news"}}; console.log(getKeyName("FOXNEWSW")); console.log(getKeyName("foo"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.