Trapped in a Javascript rut

view image description here

for each date in the $scope.all_date array, a holiday event is created with specific details such as color and start date. However, only the last date in the array is currently being displayed as a holiday event on the frontend. The goal is to display all dates as holidays until the loop reaches its end.

In order to achieve this functionality, adjustments need to be made in the code logic to properly represent each date from the backend as a holiday event in the calendar.

View Front End View Back End

Answer №1

const holidayEvents = [];
for(let index = 0; index < $scope.all_date.length; index++) {
   const holidayEvent = {
      id: '',
      color: 'red',
      borderColor: '#d2e04f',
      titleDateFormat: '',
      content:'Holiday',
      startDate: new Date($scope.all_date[index].date)
   };

   holidayEvents.push(holidayEvent);
}

Answer №2

It's important to create a fresh object during each iteration. Currently, a new object is being created for every loop, but it ends up being replaced by the current value.

Try utilizing the array#map method, which generates a new array of objects for you.

$scope.all_date.map(function(item) {
    var events = {
        id: '',
        color: 'red',
        borderColor: '#d2e04f',
        titleDateFormat: '',
        content: 'Holiday',
        startDate: new Date($scope.all_date[j].date)
    }
    return events;
})

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

Implementing a universal (click) attribute for Angular 2 in CSS

When using Angular 1, it was as easy as following this syntax: [ngClick], [data-ng-click], [x-ng-click] { cursor: pointer; } This snippet ensured that any tags with the ng-click attribute displayed a pointer cursor. How can we achieve the same effect ...

Is there a way to create a hover effect on a sidebar element?

I'm struggling to add a hover effect to the elements within my sidebar. I've tried using CSS :hover, but it doesn't seem to be working as expected. Any guidance or solution would be greatly appreciated. .sidebar .navbar .nav-link:hover{ ...

Can the (Bootstrap) popover cause a button to shift position?

<- this is where my website is located After clicking the '*로그인' button, it seems to redirect to the '*가입하기' button. Is there a way to prevent the button from moving when clicked? *('로그인' translates t ...

Styling a Kendo Dropdownlist with JavaScript

Incorporating Jquery Kendo Dropdownlist to showcase information in a list has been quite beneficial. My current objective is to introduce the mentioned styles (.k-list-container and .k-list-scroller) into the Dropdownlist via JavaScript. Moreover, I aim t ...

Submitting an HTTP POST REQUEST with both an image and text

Is there a way to send an image with text from VueJs to an ExpressJs backend? I've currently implemented two HTTP POST requests for this process. Please note: "this.albumName" and "this.albumDesc" contain text, while the formData variable holds the ...

What are the differences between Angular directives with and without square brackets?

I came across an interesting tutorial that showcases the implementation of a tooltip directive in Angular: CASE A: Tooltip without brackets <p tooltip="Tooltip from text">Tooltip from text</p> CASE B: Tooltip with brackets <p [toolt ...

Steps for displaying a confirmation popup and redirecting to a new route using AngularJS

Is it possible to display a confirmation popup with "Yes" and "No" buttons every time there is a route change in my AngularJS App? I attempted the following approach. This event gets triggered before the route change, but despite selecting 'NO', ...

Struggling with transferring information up the React component hierarchy to manipulate the Document Object Model

** I'm still learning React, so any assistance is greatly appreciated! ** A Little Background I have a modal (bootstrap4) hidden within the main app component, containing a form. The form is automatically filled with information from the selected re ...

How can I include 'res.addHeader("Access-Control-Allow-Origin", "*")' in an express js application?

I am currently using AngularJS and Cordova for the front-end of my app, and Express and Node.js for the backend which acts as the server. The client side is running on http://localhost:9000, while Express.js is running on http://localhost:3000. I am trying ...

Troubleshooting the buttons functionality in the angular-datatables plugin

I'm currently utilizing angular-datatables from the source available at: My attempt is to execute the 'with buttons' example from this link: Although I am following the example precisely, I am unable to see any buttons on the table as expe ...

Utilize computed fields in Parse Object queries

Currently, I am working on developing a Parse Server API for a cooking-centered social network. One of the key features I am focusing on is creating a function that retrieves recipes. I want this function to be able to display calculated fields such as t ...

Select the input based on the name and value provided from a radio button using jQuery

I want to show a div element when the user chooses option 4 from a radio button. $(document).ready(function () { $("#GenderInAnotherWay").hide(); $("input[name='Gender'][value=4]").prop("checked", true); $("#GenderInAnotherWay").tog ...

Android browser experiences a sudden surge of unexpected data influx

I am facing an issue with my application where it maps an array from the state. The array should ideally only contain 6 sets of data, as limited by the backend. However, sometimes it spikes and displays data that is not supposed to be there or shows old da ...

Error: The ng-click directive is encountering a parsing syntax error. The token 'Object' is unexpected and is causing the error, it is expected to be enclosed in

When a user clicks on a point on a Google map, I am conducting reverse geocoding in the following manner: geocoder.geocode({'location': latlng}, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { ...

What is the best way to display a message on the 403 client side when an email sending fails?

I am attempting to display an alert message when the email is sent successfully or if it fails. If it fails, I receive a 403 status code from the backend. However, I am unsure how to handle this error on the client-side. In the case of success, I receive a ...

React useEffect only retrieves the last element of an array object

I am facing an issue where React seems to only save the last element in my array. Even though I have two elements, when mapping over them, only the last element is being placed in the hook each time. React.useEffect(() => { if (bearbeiten) { handleCli ...

Does Ext js have a monochromatic theme of blue running through everything

While the blue color may give off a nice office ambiance, it's important to have variety in the appearance of different applications. Is it simple to customize the look in ext js? ...

What improvements can be made to optimize this SQL query and eliminate the need for an additional AND statement at the end

I am working on dynamically constructing a SQL query, such as: "SELECT * FROM TABLE WHERE A = B AND C = D AND E = F" Is there a more efficient way to construct this SQL query without adding an extra AND at the end? Here is my current code snippet: le ...

Rendering components based on a condition of a true or false prop value

Here's a code block that I need help with: return ( <StyledActiveOptions className={classNames("lookup-active-options form-control", className)} role="button" tabIndex="0" aria-haspopup=&q ...

The ng-view in index.html is not being loaded by AngularJS

I've been working on setting up a single-page application using AngularJS. In my setup, I'm using Node with Express and have an Apache server functioning as middleware. The issue I'm facing is that while my index.html page loads without any ...