简体   繁体   English

返回值是包含与条件匹配的属性的对象的键

[英]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.

相关问题 从密钥与javascript和下划线匹配特定条件的对象返回键值对 - return key value pair from object whose key matches certain condition with javascript and underscore 获取其子属性包含值的对象 - Get object whose sub-property contains value 返回键和对象中的属性值 - Return key & value of property in object 如果对象包含真键,则返回布尔真值,不返回值 - Return boolean true value if object contains true key, not return the value 如何检查对象是否包含至少一个其值包含JavaScript子字符串的键? - How can I check if an object contains at least one key whose value contains a substring in JavaScript? 返回具有最小属性值的 object 键 - Return object key with minimum property value LoDash从子数组中找到其属性与对象数组中的值匹配的对象 - LoDash find the object whose property matches the value from array of objects with children array 从与嵌套在对象中的对象的key:value对匹配的数组中返回对象,我需要返回 - Return object from array that matches key:value pair of an object nested in the object I need to return 获取符合条件的对象的第一个属性 - Get first property of an object that matches the condition 将嵌套 object 的属性键传递给 function 以返回该属性值 - passing property key for nested object to a function to return that property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM