Lodash:return 其值(即数组)中包含给定元素(即字符串)的对象的第一个键
Lodash : return first key of object whose value(i.e Array) has a given element (i.e string) in it
我有一个像这样的对象:
var obj = {
"01": ["a","b"],
"03": ["c","d"],
"04": ["e","c"]
};
并且我知道对象键值的数组元素(比如 "c")然后 如何使用 lodash 找到第一个键值,即“03”而不使用 if else?
我用 lodash 试过这样,否则:
var rId = "";
_.forOwn(obj, function (array, id) {
if (_.indexOf(array, "c") >= 0) {
rId = id;
return false;
}
});
console.log(rId); // "03"
预期结果:第一个键,即“03”,如果元素匹配其他“”。
看到评论后:现在我也很好奇
我是否需要使用原生 javascript(如果我们使用超过 2 个 if 块,则程序难以阅读)或 lodash 方式(一行中易于阅读的程序解决方案)?
由于您只想要一种能够使用简单的 Lodash 命令找到密钥的方法,因此以下应该可行:
_.findKey(obj, function(item) { return item.indexOf("c") !== -1; });
或者,使用 ES6 语法,
_.findKey(obj, (item) => (item.indexOf("c") !== -1));
这个returns“03”就是你的例子。
谓词函数 - findKey()
的第二个参数 - 可以自动访问键的值。如果没有找到与谓词函数匹配的内容,则返回 undefined
。
findKey()
的文档是 here。
文档中的示例:
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'
作为替代解决方案:考虑使用 Object.keys
和 Array.some
函数的本机 Javascript 方法:
var obj = {"01": ["a","b"],"03": ["c","d"],"04": ["e","c"]},
search_str = "c", key = "";
Object.keys(obj).some(function(k) { return obj[k].indexOf(search_str) !== -1 && (key = k); });
// the same with ES6 syntax:
// Object.keys(obj).some((k) => obj[k].indexOf(search_str) !== -1 && (key = k));
console.log(key); // "03"
这是来自未来的单一班轮答案。目前只适用于 Firefox 47 上。 ES7 提案的一部分。
var obj = {
"01": ["a","b"],
"03": ["c","d"],
"04": ["e","c"]
},
res = Object.entries(obj).find(e => e[1].includes("c"))[0];
document.write(res);
具有讽刺意味的是,没有任何库,实施起来并不困难。
Object.keys(obj).filter(x => obj[x].includes("c"))[0]
我有一个像这样的对象:
var obj = {
"01": ["a","b"],
"03": ["c","d"],
"04": ["e","c"]
};
并且我知道对象键值的数组元素(比如 "c")然后 如何使用 lodash 找到第一个键值,即“03”而不使用 if else?
我用 lodash 试过这样,否则:
var rId = "";
_.forOwn(obj, function (array, id) {
if (_.indexOf(array, "c") >= 0) {
rId = id;
return false;
}
});
console.log(rId); // "03"
预期结果:第一个键,即“03”,如果元素匹配其他“”。
看到评论后:现在我也很好奇
我是否需要使用原生 javascript(如果我们使用超过 2 个 if 块,则程序难以阅读)或 lodash 方式(一行中易于阅读的程序解决方案)?
由于您只想要一种能够使用简单的 Lodash 命令找到密钥的方法,因此以下应该可行:
_.findKey(obj, function(item) { return item.indexOf("c") !== -1; });
或者,使用 ES6 语法,
_.findKey(obj, (item) => (item.indexOf("c") !== -1));
这个returns“03”就是你的例子。
谓词函数 - findKey()
的第二个参数 - 可以自动访问键的值。如果没有找到与谓词函数匹配的内容,则返回 undefined
。
findKey()
的文档是 here。
文档中的示例:
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'
作为替代解决方案:考虑使用 Object.keys
和 Array.some
函数的本机 Javascript 方法:
var obj = {"01": ["a","b"],"03": ["c","d"],"04": ["e","c"]},
search_str = "c", key = "";
Object.keys(obj).some(function(k) { return obj[k].indexOf(search_str) !== -1 && (key = k); });
// the same with ES6 syntax:
// Object.keys(obj).some((k) => obj[k].indexOf(search_str) !== -1 && (key = k));
console.log(key); // "03"
这是来自未来的单一班轮答案。目前只适用于 Firefox 47 上。 ES7 提案的一部分。
var obj = {
"01": ["a","b"],
"03": ["c","d"],
"04": ["e","c"]
},
res = Object.entries(obj).find(e => e[1].includes("c"))[0];
document.write(res);
具有讽刺意味的是,没有任何库,实施起来并不困难。
Object.keys(obj).filter(x => obj[x].includes("c"))[0]