- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个像...这样的对象
let object = {
person: {
pet: {
name: "Rover"
}
}
};
我想使用 lodash
(或任何与此相关的东西)来进行检查,例如...
Is the value at
object.person.pet.name
equal to"Spot"
?
目前我能看到的唯一选择是从对象中获取宠物名称,然后检查它是否等于“Spot”。
是否有一个函数可以用来进行一次检查,例如......
_.equals(object, 'person.pet.name', "Spot");
如果它通过 lodash
存在或不存在,那就太好了。
我目前使用 lodash
的原因是我不知道人、宠物或名字是否真的存在,因此 lodash 为我提供了一种“安全”获取该数据的方法。
最佳答案
lodash中似乎不存在这样的方法。
无论如何,您可以使用 findKey
方法来查找符合给定要求的 key 。
_.findKey(object, { 'person.pet.name' : 'Spot' });
如果它返回未定义
,则意味着它不相等。
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'
<小时/><小时/>
非loadash自定义函数
const object = {
person: {
pet: {
name: 'Rover',
},
},
};
function checkEquality(obj, key, value) {
const val = key.split('.')
.reduce((tmp, x) => (tmp !== void 0 && tmp[x] !== void 0 ? tmp[x] : void 0), obj);
if (val === void 0) return false;
return val === value;
}
console.log(checkEquality(object, 'person.pet.name', 'Rover'));
console.log(checkEquality(object, 'person.pet.name', 'RoverTT'));
console.log(checkEquality(object, 'person.petak.name', 'Rover'));
关于node.js - lodash 检查 object.path 是否等于值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833471/
使用lodash,如何将生日相同的人的姓名归为一组,如下所示? 我使用嵌套的for循环写出了这个信息,但我想在lodash中会有一种更优雅的方法。我没有使用太多,并试图找出要使用的功能。 [
嘿,我想使用 lodash 计算对象数组中一个属性的总和 假设对象数组看起来像这样...... salary":[{ "bills":[{"electricity":300,"milk":500
我试图在调整窗口大小时触发一个事件,它似乎不起作用。 $(window).resize(function(){ _.debounce(function(){ console.log
我正在使用 StencilJS 编写一个网络组件。由我的组件导入的辅助类之一 (ArrayHelper.ts) 在顶部有这些导入: import transform from 'lodash/tran
我想用比较器对数组进行排序,例如: function myCustomComparator(a, b){...} 好像是 var sorted = myArray.sort(myCustomCompa
我试图弄清楚 Lodash iteratee 的工作原理以及我将在哪里使用它。 The documentation says: Creates a function that invokes func
我是 lodash (v3.10.1) 的新手,很难理解。希望有人能帮忙。 我有这样的输入: { {"id":1,"name":"Matthew","company":{"id":1,"na
我正在尝试添加到流类型的类型中。我从 lodash/fp 模块开始,因为它对我自己最有用。但是,我正在为如何正确输入它而苦苦挣扎。 如果 dropRightWhile 的一个简单示例: declare
有没有办法获取 lodash API 文档的 2.x 版本?上周末似乎主要链接已切换到 3.x,无法查看 2.x 文档。 https://lodash.com/docs 最佳答案 您可以在 Inter
为了减少 Angular 项目的包大小,我正在安装和导入单独的 lodash 库。 IE。 lodash.clonedeep 和其他 lodash.* 但会丢失这些的类型定义,因为它们不能与 @typ
像这样从 lodash 导入"template"有什么区别: import { template } from 'lodash'; 或者像这样: import template from 'lodas
我看到导入整个 lodash 库占用了相当多的磁盘空间: $ du . | grep lodash 1696 ./lodash/fp 5000 ./lodash 在我的代码中我只是在做 r
According to the lodash docs , _.extend(object, [sources]) 改变第一个参数。 var dest = { a: 1 }; _.extend(
我想创建值为“str”的键数组我有以下嵌套 json: [ { "Attr": [ { "power": { "p1": "str", "t3": "str"
lodash 提供了一个方法 _.uniq()从数组中查找唯一元素,但使用的比较函数是严格相等 === , 而我想使用 _.isEqual() ,满足: _.isEqual([1, 2], [1, 2
是escape和 unescape underscore 的功能和 lodash相同?我也可以吗escape与 lodash和 unescape与 underscore并且总是得到相同的字符串? 我可
我知道 rxjs 和 lodash 的定义和职责,但我想知道: 当我在我的项目中使用 rxjs 时,我可以扔掉 lodash 吗? 因为 rxjs 可以同步和异步工作(异步和同步数据)。我认为它可以作
使用 lodash 我想找到一个 id 为 3229 的团队。我尝试关注但它没有返回任何内容。 var team = _.chain(data.teams) .flatten(
我想知道 2 次导入在内存和性能方面是否有任何差异。 如果我的节点模块中有 lodash,无论导入如何,它都会编译所有文件吗? 最佳答案 理论上,based on the specification
基本上,我有一个对象数组,我只想更新数组中满足条件的对象。我想知道是否有解决该问题的有效方法。现在我正在使用 lodash。这是一个例子: var things = [ {id: 1, typ
我是一名优秀的程序员,十分优秀!