How to know the difference between an array and another?

Asked

Viewed 37 times

0

import { Injectable } from "@angular/core";

import { Show } from "./shows/show";
// import { SHOWS, DISPLAYING_SHOWS } from './shows/shows';

export const SHOWS: Show[] = [
  {
    name: "Arrow",
    seasons: [
      {
        seasonNumber: 1,
        episodes: [
          {
            numberInShow: 0o1,
            seasonNumber: 0o1,
            numberInSeason: 0o1,
            episodeName: "Pilot",
            watched: false,
          },
        ],
      },
      {
        seasonNumber: 2,
        episodes: [
          {
            numberInShow: 24,
            seasonNumber: 0o2,
            numberInSeason: 0o1,
            episodeName: "Episode",
            watched: true,
          },
        ],
      },
    ],
  },
];

export const DISPLAYING_SHOWS: Show[] = [];

export let REST_SHOWS: any[] = [];

@Injectable({
  providedIn: "root",
})
export class ShowService {
  constructor() {
    // let r: any[] = [];
    let s: any[] = [];
    let d: any[] = [];

    for (const show of SHOWS) {
      s.push(show.name);
    }

    for (const show of DISPLAYING_SHOWS) {
      d.push(show.name);
    }

    // for (let index = 0; index < s.length; index++) {
    //   if (d.includes(s[index])) r.push(s[index]);
    // } // old, mas com preguiça

    // REST_SHOWS = r;
    REST_SHOWS = REST_SHOWS.filter(x => !s.includes(d));
  }

  getDisplayingShows(): Show[] {
    return DISPLAYING_SHOWS;
  }

  addShow(show: Show) {
    DISPLAYING_SHOWS.push(show);
    REST_SHOWS.splice(REST_SHOWS.indexOf(show.name), 1);
  }

  deleteShow(show: Show) {
    // const key = undefined;
    // for (const s of DISPLAYING_SHOWS) {
    //   const index = DISPLAYING_SHOWS.indexOf(s);
    //   DISPLAYING_SHOWS.splice(index, 1);
    // }
    REST_SHOWS.push(show.name);
    DISPLAYING_SHOWS.splice(
      DISPLAYING_SHOWS.indexOf(this.getDisplayingShow(show.name)),
      1
    );
  }

  getDisplayingShow(name: string) {
    for (const s of DISPLAYING_SHOWS) {
      if (s.name.toLowerCase() === name.toLowerCase()) return s;
    }
    return null;
  }

  isDisplaying(name: string) {
    return this.getDisplayingShow(name) !== null; // !== null === true; === null === false
  }

  isShowDisplaying(show: Show) {
    return DISPLAYING_SHOWS.includes(show);
  }

  updateShow(show: Show) {
    this.deleteShow(show);
    this.addShow(show);
  }

  // Database
  getRestShows(): Show[] {
    let rest: Show[] = [];
    for (const r of REST_SHOWS) {
      rest.push(this.getShow(r));
    }
    return rest;
  }

  getShows(): Show[] {
    return SHOWS;
  }

  getShow(name: string) {
    for (const show of SHOWS) {
      if (show.name === name) return show;
    }
    return null;
  }

  getShowByClass(show: Show) {
    for (const s of SHOWS) {
      if (s.name === show.name) return s;
    }
    return null;
  }

  existsShow(name: string) {
    return this.getShow(name) !== null;
  }
}

I want to take the shows that are not on DISPLAYING_SHOWS, as if it were a subtraction:

shows - displaying_shows = Rest;

Whenever I delete something from displaying_shows, Rest keeps the same value as before.

No error occurs on console.

  • One could explain by putting two arrays and a array resulting? do not need to put so... (Note: what names of names are these, have no pattern?). You have more problems than just what’s in the question!.

  • 1

    ... It’s like I said put two array and the array of the result, which we can help... (really the subtraction was not clear to me at least)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.