Look for a specific key value that appears in various parent keys throughout an object in JavaScript

Imagine we have an object that contains information about fruit quantities in different batches:

"fruit_quantities" : {
  "batch1": {
    mango : 100,
    apple: 50
  },
  "batch2": {
    mango : 0,
    apple: 50
   },
  "batch3": {
    mango : 0,
    apple: 50
   }
}

I am interested in checking if the value of mango is greater than 0, regardless of the batch it is in. How can I achieve this using JavaScript?

Answer №1

To achieve this, you can utilize the power of Object.keys() in combination with the Array.prototype.some() method as shown below:

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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it feasible to achieve a concept similar to "non-standard evaluation" in R using Javascript or Python?

When working in R, you have the ability to create functions that can handle unquoted attributes of a pre-defined object as arguments. For instance, the DataFrame object interface allows for operations like: # df has columns "A" and "B" df = mutate(df, C=A ...

Using ajax to call the Google Maps Api is proving to be ineffective

I am facing some issues with a website. On this particular webpage (), I am trying to display a Google map on the location page using an AJAX function. The getLocation.php file is being called by AJAX: <?php echo '<div id="map-canvas"></ ...

avoiding the duplication of effects on an object when new objects are added via ajax

Currently, I am facing a minor issue with an application that I am working on. The problem arises on a particular page where the user can edit a document by dragging modules onto the canvas area. Upon loading the page, JavaScript causes the modules to be ...

I am using an A-frame animation-mixer. What is the best way to seamlessly switch between different json model animation clips

Recently, I started using A-Frame and have a question regarding the animation-mixer. For my webVR project, I am animating a rigged json model. Currently, I have successfully loaded and animated the model in the idle state: <a-entity id ...

The hyperlink and criteria provided are accurate, however, the webpage remains stagnant

My login page is supposed to redirect to the contact confirmation page when I receive a 200 response from the server, but it's not working. Can you help me find my mistake? const [form, setForm] = useState({}); async function checkLogin() { tr ...

Tips for integrating global functionalities such as Create, Methods, and Mounted from a plugin into Vue

In certain scenarios, I prefer not to utilize mixins in my Plugin. Instead, I am working on incorporating custom methods like `created()`, `mounted()`, and `methods{}` so that I can access its property when the component loads and execute my own custom me ...

Updating the JSON format output from snake case to camel case in a React web application

Modifying JSON output in a React Web app to change keys from snake case to camel case Currently, the API endpoint response is structured like this: [ { "id": 1, "goals_for": 0, "goals_against": 0, "points": 0 } ] ...

How can I ensure that the HTML I retrieve with $http in Angular is displayed as actual HTML and not just plain text?

I've been struggling with this issue for quite some time. Essentially, I am using a $http.post method in Angular to send an email address and message to post.php. The post.php script then outputs text based on the result of the mail() function. Howev ...

Avalara: Can you provide an acceptable format for the "DateTime" in a JSON date?

What is the appropriate JSON date format for Avalara? In the given code snippet: TransactionModel transaction = new TransactionBuilder(client, "COMPANY", DocumentType.SalesOrder, "myCompany.") .withDate(Calendar.getInstance().getTime()) .withAddr ...

Displaying MQTT feedback on a node.js server endpoint

I am currently working on setting up a node server using express to output the response received from MQTT. Every second, a JSON string message is sent from the mqtt service. This response will be displayed on the /main API, which I will access from an Io ...

Package for validating forms in Meteor using React

Looking for recommendations on a Meteor React form validation package or custom validation code that's compatible with components. Need something similar to jQuery validate, but specifically designed to work seamlessly with Meteor and React. ...

Enhancing Django Datatables by sending additional data from server to template

After successfully implementing the dataTables plugin with django server-side processing, my code looks something like this: In the template: <script type="text/javascript"> $(document).ready(function() { $('#example').dataTable( ...

Step-by-Step Guide: Exporting a div to a beautifully formatted PDF with NextJs

Currently, I am working on an app project that involves filling out forms and having the information automatically formatted into a div to generate CLP labels. My goal is to be able to export this div in pdf format while also being able to select sizes ran ...

Everlasting Dropdown in AngularJS Always Open Mode

I am currently working on my first AngularJS App and I am facing some issues with creating a Dropdown menu. Here is the HTML code I have: <div class="btn-group" dropdown> <button type="button" class="btn btn-danger">Action</button> & ...

Can you explain the distinctions between using this['key'] and $data['key'] in the context of v-model?

Take a look at the snippet below, which includes three demos. The first two demos are functioning correctly (v-model is working fine). However, in the last demo, when you type something into the <input>, you will notice that this['test 1'] ...

How can I ensure that an HTML element is not displayed until the associated variable in the JS is loaded in AngularJS?

Below is the HTML code snippet I am working with: <button type="button" class="btn btn-primary" ng-hide="ctrl.userProfile.following">Follow</button> <button type="button" class="btn btn-primary" ng-show="ctrl.userProfile.following">Unfol ...

What are some creative ways to enhance closePath() in canvas styling?

Currently, I am faced with the challenge of styling the closePath() function on my canvas. Specifically, I would like to apply different stroke styles to each line segment. For now, let's focus on changing colors for each line. In the example below, y ...

Rotating an object in Three.js: Animate back and forth along two different azimuth angles

My three.js project features a 3D object that is meant to be viewed from the front only, as it appears as a single plane and is transparent from the back... Using orbitControls, I have restricted movement of both azimuth and polar angle... To enhance the ...

Identify React Server-Side Component

How can we distinguish between a file or function being executed as part of a React Server Component rendering versus a classic client component rendering ("use client";)? ...

Using Typescript with Protractor for Dropdown Menus

As a newcomer to Protractor, I am looking to automate the selection of a dropdown. While I have some knowledge in JavaScript, I am currently working with typescript. Can someone advise me on how to select the dropdown option based on the text provided? Fo ...