1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Arrays's Objects property is either true or false but becomes always false when accessed in a...

Discussão em 'Angular' iniciado por Ahmad Raza, Junho 10, 2025.

  1. Ahmad Raza

    Ahmad Raza Guest

    I'm facing an unexpected situation. I have some objects(array). Those have a property break, that is boolean. break value depends upon situation, but it becomes always false when compared in if statement, in a for or for of loop.

    I tried to change the style of if by using ==, === and only property, but all do the same result, even when some of objects have value true. Simply saying, when the array of object is iterated, the object's break property becomes false, even if it it true. Language is TypeScript.

    Assume getRoute() method is called.

    This is the method:

    import { Injectable } from '@angular/core';
    import { of, EMPTY, concat } from 'rxjs';
    import { HttpService } from '../http.service';
    import { take, tap, map, last } from 'rxjs/operators';

    @Injectable({
    providedIn: 'root'
    })
    export class RouteService {
    activities;
    distanceDB;
    cityFilter = [];
    route: any[];

    constructor(private httpService: HttpService) { }

    setActvities(activities) {
    this.activities = activities;
    }

    getRoute() {
    if (this.activities) {
    return concat(
    this.sortUpActivities(),
    this.calculateBreaks(),
    this.allocateNights()
    ).pipe(
    last()
    );
    }

    return EMPTY;
    }

    /* sort up activities according to city */
    private sortUpActivities() {
    const route = [];
    let counter = 0;

    for (let i = 0; i < this.activities.length; i++) {
    for (let j = 0; j < this.activities.length; j++) {
    const city = route.find(r => {
    return r.cityCode == this.activities[j].cityCode;
    });

    if (!city) {
    route[counter] = {}
    Object.defineProperty(route[counter], 'break', {
    value: false,
    writable: true
    });

    route[counter].activities = [this.activities[j]];
    route[counter].cityCode = this.activities[j].cityCode;
    counter++;
    } else {
    city.activities.push(this.activities[j]);
    }
    }
    }

    this.route = route;
    return of(this.route);
    }

    /* calculate breaks required in each city */
    private calculateBreaks() {
    let timeSincePreviousBreak = 7;
    let transitTime = 0;

    return this.httpService.getCityToCity(this.cityFilter).pipe
    (
    take(1),

    tap<any[]>(cityToCity => {
    for (let i = 0; i < this.route.length; i++) {
    let totalActivityTime = 0;;
    for (const ea of this.route.activities) {
    totalActivityTime += ea.duration;
    }

    if (this.route[i+1]) {
    const cityToCityInfo =
    cityToCity.find(c => c.from == this.route.cityName
    && c.to == this.route[i+1].cityName) ||
    cityToCity.find(c => c.from == this.route[i+1].cityName
    && c.to == this.route.cityName);

    if (cityToCityInfo) {
    transitTime = cityToCityInfo.time || 0; // `|| 0` because of DB probable problem
    } else {
    transitTime = 0; // `|| 0` because of probable DB problem
    }
    } else {
    transitTime = 0; // because it is perhapse last city
    }

    const totalTime = timeSincePreviousBreak + totalActivityTime + transitTime;

    this.route.activityTime = totalActivityTime;
    if (totalTime >= 9) {
    this.route.break = true;
    timeSincePreviousBreak = transitTime;
    } else {
    this.route.break = false;
    timeSincePreviousBreak = totalTime;
    }
    }
    }),

    map(cityToCity => { return this.route })
    );
    }


    /* allocate nights according to breaks */
    private allocateNights() {
    for (let i = 0; i < this.route.length; i++) {
    if (this.route.break) {
    Object.defineProperty(this.route, 'nights', {
    value: 0,
    writable: true
    });

    if (this.route.activityTime <= 7) {
    this.route.nights = 1;
    } else if (this.route.activityTime <= 14) {
    this.route.nights = 2;
    } else {
    this.route.nights = 3;
    }
    } else {
    console.log(this.route.break);
    }
    }

    return of(this.route);
    }
    }


    These are the entries in the log:

    Angular is running in the development mode. Call enableProdMode() to enable the production mode. core.js:38781
    (5) […]

    0: {…}

    activities: Array [ {…} ]

    activityTime: 3

    break: true

    cityCode: "ELL"

    <prototype>: Object { … }

    1: {…}

    activities: Array [ {…} ]

    activityTime: 5

    break: false

    cityCode: "NUW"

    <prototype>: Object { … }

    2: {…}

    activities: Array [ {…}, {…} ]

    activityTime: 10

    break: true

    cityCode: "KAN"

    <prototype>: Object { … }

    3: {…}

    activities: Array [ {…} ]

    activityTime: 5

    break: false

    cityCode: "YAL"

    <prototype>: Object { … }

    4: {…}

    activities: Array(3) [ {…}, {…}, {…} ]

    activityTime: 12

    break: true

    cityCode: "GAL"

    <prototype>: Object { … }

    length: 5

    <prototype>: Array []
    route.service.ts:118:12

    ----------------------------------------------------
    false 5 route.service.ts:135:16
    ----------------------------------------------------
    (5) […]

    0: {…}

    activities: Array [ {…} ]

    activityTime: 3

    break: true

    cityCode: "ELL"

    <prototype>: Object { … }

    1: {…}

    activities: Array [ {…} ]

    activityTime: 5

    break: false

    cityCode: "NUW"

    <prototype>: Object { … }

    2: {…}

    activities: Array [ {…}, {…} ]

    activityTime: 10

    break: true

    cityCode: "KAN"

    <prototype>: Object { … }

    3: {…}

    activities: Array [ {…} ]

    activityTime: 5

    break: false

    cityCode: "YAL"

    <prototype>: Object { … }

    4: {…}

    activities: Array(3) [ {…}, {…}, {…} ]

    activityTime: 12

    break: true

    cityCode: "GAL"

    <prototype>: Object { … }

    length: 5

    <prototype>: Array []
    route.service.ts:139:12

    Continue reading...

Compartilhe esta Página