简体   繁体   English

检查键的值,该值存在于 javascript 对象内的许多不同的父键中

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

相关问题 如何检查对象中存在的键的实例 - How to check instance of key which is present inside object 如何获取 Javascript 中 object 中存在的密钥的值 - How to get the value of the key present inside an object in Javascript javascript 检查数组的值是否作为 object 中的键存在 - javascript check if the values ​of an array are present as a key in an object 如何访问在 javascript 的 object 的键内定义的 function 中的值 - how to access the value in a function which is defined inside a key of an object in javascript 遍历获取密钥和所有父密钥的对象 - Traversing an object getting the key and all parents keys 省略存在于javascript中另一个数组中的对象中的键 - Omit keys from an object which are present in another array in javascript Handlebars 检索对象值,其中键将出现在另一个对象中 - Handlebars retrieve object value in which the key will be present in another object 检查数组中有多少元素作为javascript对象中的键存在 - Check how many elements of an array exists as keys in an object in javascript 如果存在于数组的 object 中,则特定键的计数 [JavaScript] - Count Of a specific key if present inside a object of array [JavaScript] 如何检查另一个对象内的对象上是否存在值? - How to check if a value is present on a obect inside another object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM