[英]Check for a value of key, which is present in many different parents keys inside a object in javascript
Lets consider we have a object as shown below ,让我们考虑一下我们有一个如下所示的对象,
"fruit_quantities" : {
"batch1": {
mango : 100,
apple: 50
},
"batch2": {
mango : 0,
apple: 50
},
"batch3": {
mango : 0,
apple: 50
}
}
I just need to find if the value of mango
is greater than 0, irrespective of which batch
is it in. How would I do this in javascript?我只需要找出
mango
的值是否大于 0,而不管它在哪个batch
中。我将如何在 javascript 中执行此操作?
You can do it using the combination of Object.keys()
and Array.prototype.some()
method like this:你可以使用
Object.keys()
和Array.prototype.some()
方法的组合来做到这一点:
var fruit_quantities = { "batch1": { mango : 100, apple: 50 }, "batch2": { mango : 0, apple: 50 }, "batch3": { mango : 0, apple: 50 } }; var fruit = "mango"; var r = Object.keys(fruit_quantities).some(function(key) { return fruit_quantities[key][fruit] > 0; }); console.log(r);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.