Wede Asmera Tseada
Wede Asmera Tseada

Reputation: 533

array value reevaluated after assignment

I am learning Angular and getting lost on this specific call. I have to make 3 calls to local JSON files:

    const one$ =this.http
    .get(
        "/assets/files/one.json"
    ).map((res) => res).catch(e => Observable.of(null));


    const two$ =this.http
    .get(
        "/assets/files/two.json"
    ).map((res) => res).catch(e => Observable.of(null));

    const three$ =this.http
    .get(
        "/assets/files/three.json").map((res) => res).catch(e => Observable.of(null));

    forkJoin([one$, two$, three$]).subscribe(results => {
      // results[0] is one
      // results[1] is two
      // results[2] is three
      let Data = null;
      console.log(results);
      //results outputs the following now
       [null, Array(35), Array(35)]

      if (results[0] != null){
        Data = results[0];
      }
      else if (results[1] =! null){
        Data = results[1]; // expected array values here but i get boolean true
      }
      else if (results[2] =! null) {
        Data = results[2]
      }
    });

When execution is finished, Data is true instead of the arrays. See the console below:

enter image description here

Upvotes: 0

Views: 42

Answers (1)

bryan60
bryan60

Reputation: 29335

you have this:

else if (results[1] =! null){

what you need is:

else if (results[1] !== null){

you're getting true because single = is assignment in javascript, so you're assigning !null to results[1] in the first case, which evaluates to true. you need a comparison operator which is either != or preferably !== as !== is a strict type comparison. but since this is typescript/javascript, you really could just do:

else if (results[1]) {

as it evaluates to "truthy" in this case, while null is "falsey". This is the simplest / cleanest way IMO

Upvotes: 2

Related Questions