Guide to stopping available times from cycling through an array with the help of watch功能?

I am currently developing a booking application within Vue CLI. I have made use of vue-ctk-date-time-picker for selecting the date and time. My goal is to disable certain times based on the chosen date, but I am encountering an issue. The code I have written only disables the times for the last specified date in the array and ignores the others.

When I log the times array to the console based on the date, it shows the correct values. Additionally, there are no errors displayed in the console.

<VueCtkDateTimePicker only-date v-model="date"/>
<VueCtkDateTimePicker only-time :disabled-hours="disabledHours"/>
date: null,
disabledHours: [],
testArray: [
  {
    date: "2019-05-28",
    times: ["10", "11"]
  },
  {
    date: "2019-05-29",
    times: ["10", "11", "12"]
  }
]

watch: {
  date(newVal, oldVal) {
    for (let i = 0; i < this.testArray.length; i++) {
      if (newVal == this.testArray[i].date) {
        for (let j = 0; j < this.testArray[i].times.length; j++) {
          this.disabledHours.push(this.testArray[i].times[j]);
        }
      } else {
        this.defaultHours();
      }
    }
  }
},
created() {
  this.defaultHours();
}

defaultHours() {
  this.disabledHours = ["00","01","02","03"]
}

If the date is "2019-05-28", then I expect the disabled hours to be 10 and 11. If the date is "2019-05-29", then I expect the disabled hours to be 10, 11, and 12, and so forth.

However, what actually occurs is that it only takes the last date specified in the array and disables its hours exclusively.

Answer №1

Your posted code will iterate through all elements in the testArray and perform a certain action for each element. If you only want the code to act on a matching entry, with a default action if no match is found, you can achieve this using the following approach:

date(newValue) {
    const matched = testArray.find(entry => entry.date === newValue);
    if (matched) {
        this.disabledHours = matched.times;
    } else {
        this.defaultHours();
    }
}

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

When using i18next interpolation in React components, the displayed output may show as [object Object]

In my React application, I am utilizing the next-i18next package. I want to include a React component within my interpolation: import React from 'react'; import { useTranslation } from 'next-i18next'; import { serverSideTranslations } f ...

Activate click events when button is being held down

I'm currently working on a directive that shifts an element to the right whenever clicked. However, I want the element to keep moving as long as the button is pressed. .directive("car", function(){ return { restrict:"A", link:func ...

After moving a JavaScript application to heroku, the script appears to be nonfunctional

I developed a basic javascript application using mojs, an animation library, and aimed to host it on heroku. To begin with, I attempted the "heroku create" command to deploy the original app on heroku - although the app was accessible, the script did not f ...

Managing and retrieving data in bulk from an indexed database as JSON format

I am currently developing an application that utilizes IndexexDB for local data storage. I am looking for a way to extract and insert large amounts of data from an indexed DB in JSON format. Below is the code snippet illustrating what I have accomplished s ...

Implement a unique feature for specific days using jQuery UI Datepicker with a customized class

Looking to highlight a range of days horizontally in jQuery UI Datepicker with the multiselect plugin. To achieve this, I am utilizing the :before and :after pseudoelements of the a tags. .ui-state-highlight a:before, .ui-state-highlight a:after { con ...

What is the process for converting several files into a base64 string?

I have a component set up like this: <input type="file" multiple @change="toBase64Handler($event)"> <script> data() { return { files: [], } }, methods: { tobase64Handler(event) { // implementation } } </script> I w ...

Using JQuery to parse an XML file and automatically redirecting the page if a specific attribute equals "Update"

Updated I've made some edits to clarify my request. My website is built using HTML5, CSS3, and JQuery, resulting in all content being on one page. During updates, I want the ability to set an option in a configuration file so that when users visit m ...

Implement using a variable as a key for an object in a reducer function

I am facing an issue with constructing an object. The string, named "SKU" in this scenario is being passed through action as action.name. Although I have all the necessary data in the reducer function, I need to dynamically replace the hardcoded SKU with ...

Issue with idangero.us swiper: resizing problem occurs when image exceeds window size

Having an issue where resizing my window causes the image to become larger than anticipated. As a result, the current image is partially cropped on the left side while the previous image starts to show through. Here's a description of what's happ ...

Looping through two columns in Bash, only printing the first column during the first iteration, and executing a specified command on the second iteration

I need help with a bash script that involves echoing two different fields and combining the command output into a single line. In my script, I am extracting data from a csv file, specifically columns $2 (project name) and $NF (volume name). The goal is to ...

Exploring tailored markup features in Next.js version 13

I am trying to optimize my website for social media sharing on platforms like WhatsApp. I have been experimenting with different methods to set custom markup in Next.js 13, but haven't achieved the desired outcome yet. Your help and insight are greatl ...

Leverage the Redux store as the source of data for sending a POST request

For my React project, I designed an input interface and saved the data in a Redux state. Afterward, I set up a new action for making an API call to post the data. When using a constant value as the "data" parameter, everything functions properly. However ...

Bundle Thirdparty Dependencies with Webpack

I am looking to package a webpack bundle that includes all common third-party vendors such as Angular 1.4, jQuery, and other libraries. Currently, the following modules have been developed: Module A Vendor Module Vendor Module: Create a simple module ...

Using additional properties when referencing another Mongoose schema

var UserSchema = new Schema({ job : [{ type : mongoose.Schema.Types.ObjectId, experience : String, grade : String, ref: 'Company'}] }); User's profile can include multiple jobs. The process of adding a job to the user is a ...

Elegant Bootstrap 4 Carousel featuring a glimpse of the upcoming slide alongside the primary carousel item

I am in search of a straightforward Bootstrap 4 carousel that showcases a glimpse of the next slide on the right. Despite exploring similar questions, I have not found a suitable solution. The links to those questions are: 1)Bootstrap carousel reveal part ...

PHP: Assigning child key as parent key in an array transformation

Is there a way in php to replace the key in an array with the key of a sub-array to achieve this effect? `Array ( [0] => Array ( [for_a] => me ) [1] => Array ( [for_b] => you ) ...

Passing a PHP variable within a for loop

Could someone assist me in understanding how to pass a variable through a for loop? My goal is to display the variable $guess continuously until it reaches 40 and then print either "you guessed too low" or "you guessed too high". How can I link the varia ...

Prevent clicking here

I'm attempting to only prevent the click event on the current item with a certain class. $(document).on('click', '.item', function() { $(".item").removeClass('is-expanded'); $(this).addClass('is-expanded'); ...

Retrieve data from an SQL database and populate an HTML dropdown menu in a web page using PHP

I am a beginner in the world of JavaScript and facing some challenges. I am working with PHP 7 and attempting to retrieve data from an SQL database, specifically a table. My goal is to extract a single column from this table and display it in a dropdown me ...

The error message states: "An attempt was made to destructure a non-iterable object. In order for non-array objects to be iterable, they must have a [Symbol.iterator

I need to call a RTK query endpoint from a function const [getCityCode, { isLoading, error, data, isSuccess, isError }] = useLocationQuery(); const getLocationDetails = async () => { const queryItems = { latitude: lat, longitude: long }; await getC ...