Show a confirmation message to the user if the data contains a true value

I am looking to implement a confirm message using Vue.js that will only display if the data retrieved from an API contains a true value. The API output is shown below, and I have included the if condition used to trigger the alert message.

//API Data

{
    "BNG-JAY-137-003": false,
    "BNG-JAY-137-004": false, 
    "BNG-JAY-137-005": false 
}

//Below is the if condition where the API output is stored in a variable 'selected_data'

if(this.selected_data){
    if(confirm("Do you want to Turn On All lights")){
        alert("Lights on")
    }
}

Answer №1

To determine if any property within your object contains the value true, you will need to iterate through the object. You can use the following code snippet as an example:

let data = {
  "BNG-JAY-137-003": false,
  "BNG-JAY-137-004": true,
  "BNG-JAY-137-005": false
};

let hasTrue = false;

for(let key in data) {
  if(data[key]) {
    hasTrue = true;
    break;
  }
}

if(hasTrue) {
   if(confirm("Do you want to Turn On All lights")){
     alert("Lights on");
   }
}

Answer №2

Check this out.

if (Object.keys(selected_data).map(function(item) { 
    return selected_data[item]}).indexOf(true) >=0) {
       if(confirm("Would you like to Activate All lights?")) {
           alert("Lights activated")
       }
    }
}

Answer №3

Consider using the confirm method in JavaScript. This method will always return a boolean value which can be used in your code logic. You can also combine it with Object.values().

function checkAndConfirm(object) {
    let result = true;
    for (let key in Object.values(object))
        result = Object.values(object)[key] && result;

    if (result) {
        let confirmed = confirm("Do you want to turn on all lights?");
        if (confirmed) alert("Lights on")
    }
}

checkAndConfirm({ //this should evaluate as false
    "BNG-JAY-137-003": false,
    "BNG-JAY-137-004": true, 
    "BNG-JAY-137-005": true
});
checkAndConfirm({ //this should evaluate as true
    "BNG-JAY-137-003": true,
    "BNG-JAY-137-004": true, 
    "BNG-JAY-137-005": true
});

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

What would be the best TypeScript target and libs to utilize in a transpiler-free Node.js project?

If I am running a TypeScript Node.js project with the command tsc && node build/index.js (where tsc builds into build/ and index.ts is in the project), what values should be used in lib and target in tsconfig.json to ensure access to the latest TypeScrip ...

Trigger oncopy on the body excluding specific class

Is there a way to execute a function on document.body.oncopy but exclude a certain class (defined for div elements) from this function call? I want to avoid calling the function on specific classes, is there a method to achieve this? ...

What could be causing the 404 errors on my static nuxt.js application hosted on GitHub pages?

I've followed this step-by-step guide: Created a Vue project with Nuxt.js (successfully tested with npm run dev) Utilized Nuxt.js static site generation to produce a static version in the dist folder of my master repository (confirmed operational usi ...

Ways to update the color of text exceeding the block boundaries: JavaScript and CSS

When dealing with background images and white text, it's common for the text to extend beyond the boundaries of the image and become invisible. I'm looking for a way to color the overflowing text in the same shade as the background image (blue). ...

Partially accessible Angular service within a callback function

I'm currently facing an issue in my Angular simple app related to a factory that is not fully available within a callback function. You can check out a simplified version of the application on this Plunkr link. Here's a snippet of the code: Th ...

Ways to implement the function provided in the website using javascript

Exploring the world of JavaScript functionality as a beginner, I am eager to try pasting dynamic data into HTML. I came across a helpful link providing instructions on how to achieve this using a table format. However, despite my best efforts, I am strugg ...

Tips for handling the closure of a Bootstrap popup upon submission in a VUE framework

I am looking for a way to automatically hide a Bootstrap popup once the "Add Location" button is clicked and the field is filled in. <template> <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" d ...

Advancing through time with progress

How can I display a progress indicator for events in fullcalendar based on the current time by changing their color dynamically in HTML? Thank you for your help! ...

Utilizing AngularJS and Node.js to update MongoDB

Looking for a solution to update my MongoDB using Node.js and AngularJS within the front-end of my application. The code I currently have is causing an error stating `TypeError: Cannot read property 'put' of undefined. Here is my AngularJS contr ...

Tips for retrieving data from an Excel spreadsheet on an HTML/CSS webpage

I have a single HTML template at this location: . The current page is tailored for the state of Arkansas in the US, but I now need to replicate the design for all 50 states. Each state page will have the same layout, but different content specific to that ...

Tips for creating a personalized callback within a user function using JavaScript

Utilizing callbacks is a common practice when working with third-party libraries like jQuery. However, I have encountered a situation where I need to implement my own callback function. Consider the following snippet from my current code: // Get All Rates ...

Eliminating duplicate data submissions with jQuery Ajax

Encountering an issue with my jQuery AJAX submission process. JavaScript: $('#myform').submit(function () { if (validateEvenInputs()) { $('#btnevent').attr('disabled', 'disabled'); function ...

Tips on Moving a Bootstrap Modal Popup with the Arrow Keys on Your Keyboard

This example showcases the integration of Bootstrap's Default Modal Popup with jQuery UI Draggable Function. The JS fiddle link provided below contains the code snippet. $(document).ready(function() { $("#btnTest").click(function() { $(&a ...

Master the art of using useMutation from react-query: Learn how to handle error messages and success notifications effectively!

Currently in my Next.js project, I have integrated react-query to retrieve data from MongoDB. Within my form, a POST request is sent using the useMutation hook: /*** components ***/ import {Button, InputGroup} from "../index"; import {useMutatio ...

What are the methods to alter validation for a Formfield based on the input from other Formfields?

My aim is to create a Form where input fields are required only if one or more of them are filled out. If none of the fields have been filled, then no field should be mandatory. I came across a suggestion on a website that recommended using "valueChanges" ...

Bring in Bootstrap and the Carousel plugin using Webpack Encore

Currently, I am tackling an issue within my Symfony project that involves Webpack Encore and the loading of Bootstrap and the Carousel plugin. The problem may stem from how I import Bootstrap, as it seems to partially work when I import the file like this ...

The animation does not reoccur in Firefox when using Modal

I have a unique issue with my modal that displays images, where the animation only works the first time on Firefox. When I open the modal for the first time, everything works perfectly, but if I close it and then reopen it, the animation no longer plays. I ...

Tips for eliminating confidential content from adjacent/preceding entries in WordPress?

I recently incorporated a Vue.js single page application into a WordPress theme at wue-theme.app, and I am looking to implement next/previous navigation links for blog posts within the single-post template. However, I am facing challenges in excluding pr ...

Error in rendering Vue JS on Internet Explorer and Microsoft Edge

The website loads correctly on all browsers except for IE 11 and Microsoft Edge. The only error I can identify is "SCRIPT1028: Expected identifier, string or number chunk-vendors.8615b873.js (1407,41419)". Snippet of the problematic code: this.form.addE ...

dimming of dojo dijit.Dialog

I've been encountering issues with "undimming" a Dojo Dijit dialog. Even though I am using the appropriate calls, there are instances where parts of the page remain dimmed. I can still scroll through the windows and access the undimmed sections. Is th ...