Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 141277

Changing nested object value in AngularJS changes all other values

$
0
0

Consider the following nested object that loops 10 times to product a object with schedules for 10 weeks, for every monday at the given times:

$rootScope.bookings_times_list = [];
for (i = 0; i < 10; i++) {    
    $rootScope.bookings_times_list.push({
        week: i,
        schedule: [{
            booking_day: "Monday",
            booking_times: [{
                "booking_start_time": "00:15",
                "booking_end_time": "01:00",
                "booking_selected": "",
                "booking_selected_date": "",
                "booking_time": "30 Nov 2019 07:35hrs"
            }]
        }]                         
    });
}

The problem is that changing the booking_selected value to 1 as shown below changes the values for all booking_selected keys for every week to 1:

$rootScope.bookings_times_list[0].schedule[0].booking_times[0].booking_selected = 1;

Why would this happen when the key for $rootScope.bookings_times_list[0] is clearly set to 0 for just the first week?

====UPDATE====

The actual code starts off with empty booking times like this:

$rootScope.bookings_times_list = [];
for (i = 0; i < 10; i++) {    
    $rootScope.bookings_times_list.push({
        week: i,
        schedule: [{
            booking_day: "Monday",
            booking_times: []
        }]                         
    });
}

Followed by another for loop that splices Monday and adds the actual time schedule available from the booking:

for (a = 0; a < $rootScope.bookings_times_list.length; a++) {
    for (y = 0; y < $rootScope.bookings_times_list[a].schedule.length; y++) {
        if($rootScope.business_bookings[$rootScope.bookings_index].booking_times[y]){
            var index = $rootScope.bookings_times_list[a].schedule.findIndex(x => x.booking_day === $rootScope.business_bookings[$rootScope.bookings_index].booking_times[y].booking_day);

            if(index >= 0){
                $rootScope.bookings_times_list[a].schedule.splice(index, 1, $rootScope.business_bookings[$rootScope.bookings_index].booking_times[y]);
            }
         }
    }
} 

It appears the second for loop is the cause of the issue. Is the splicing affecting how the object is initialised?

===UPDATE===

I've also just noticed that it's not the splice that's the issue. Because changing from

$rootScope.bookings_times_list[a].schedule.splice(index, 1, $rootScope.business_directory_records[$rootScope.index].business_bookings[$rootScope.bookings_index].booking_times[y]);

to

$rootScope.bookings_times_list[a].schedule[index] = $rootScope.business_directory_records[$rootScope.index].business_bookings[$rootScope.bookings_index].booking_times[y]

in the second for loop still produces the same issue.


Viewing all articles
Browse latest Browse all 141277

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>