Sole
Sole

Reputation: 3340

How to remove individual object from Array in localStorage Angular

I would like to add a delete an object from an array in localStorage. What I have so far deletes the entire array, not individual objects, below is my service.ts file

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PlaylistService {

  public playlist = [];

  getPlaylist() {
    if (localStorage.getItem('playlist') == null) {
      this.playlist = [];
    } else {
      this.playlist = JSON.parse(localStorage.getItem('playlist'));
    }
  }

   deleteSongFromPlaylist(collectionId: number) {
    this.getPlaylist()
    const songIndex = this.playlist.findIndex(s => s.collectionId === collectionId)
    if (songIndex > -1) {
      this.playlist.splice(songIndex, 1)
      this.savePlaylist();
    }
  }

  savePlaylist() {
    localStorage.setItem('playlist', JSON.stringify(this.playlist));
    this.playlist = JSON.parse(localStorage.getItem('playlist'));
    console.log('Saved', this.playlist);
  }
  constructor() {
  }
}

To add an item to the array i am using the following:

component.ts

  addSongToPlaylist(itunes) {
    this.list.playlist.push(Object.assign({}, itunes));
    this.list.savePlaylist();
    console.log('Playlist - ', this.list.playlist);
  }

component.html

<div class="col-md-2 mb-5 col-lg-2 col-sm-12">
   <a target="_blank"><button type="button" class="btn btn-light btn-lg float-right"
     (click)="addSongToPlaylist(itunes)"><fa-icon [icon]="faPlus"></fa-icon>
</button></a>

playlist.ts

import { Component, OnInit } from '@angular/core';
import { PlaylistService } from '../../../services/playlist.service';
import { faSave } from '@fortawesome/free-solid-svg-icons';
import { faMinus } from '@fortawesome/free-solid-svg-icons';


@Component({
  selector: 'app-playlist-view',
  templateUrl: './playlist-view.component.html',
  styleUrls: ['./playlist-view.component.scss']
})
export class PlaylistViewComponent implements OnInit {

  faSave = faSave;
  faMinus = faMinus;
  collectionId: number;

  p: number = 1;

  constructor(private list: PlaylistService) { }

  remove() {
    this.list.deleteSongFromPlaylist(this.collectionId);
  }

  ngOnInit() {
    this.list.getPlaylist();

  }
}

playlist.html

<app-header></app-header>
<div class="container">
  <table class="table mt-3 mb-3">
    <thead class="thead-light">
      <tr>
        <th>Artwork</th>
        <th>Artist</th>
        <th>Title</th>
        <th>Genre</th>
        <th>Price</th>
        <th>Listen</th>
        <th>Edit</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let user of list.playlist | paginate: { itemsPerPage: 5, currentPage: p }">
        <td><img src="{{user.artworkUrl60}}"></td>
        <td>{{user.artist}}</td>
        <td>{{user.Name}}</td>
        <td>{{user.GenreName}}</td>
        <td>{{user.Price}}</td>
        <td><a href="{{user.collectionViewUrl}}" target="_blank">Music</a></td>

        <td><a target="_blank"><button type="button" class="btn btn-sm btn-danger"
          (click)="remove()">
          <fa-icon [icon]="faMinus">
          </fa-icon>
        </button></a></td>
      </tr>
    </tbody>
  </table>
  <div class="table-pagination mb-5">
      <pagination-controls (pageChange)="p = $event" class="my-pagination"></pagination-controls>
    </div>
</div>

<app-footer></app-footer>

Upvotes: 0

Views: 845

Answers (1)

bryan60
bryan60

Reputation: 29325

you need to get the array and then save it again, this requires you to have a method of identifying the items in the playlist to remove.

since the songs have an collectionId property of some kind...

deleteSongFromPlaylist(collectionId: number) {
  this.getPlaylist()
  const songIndex = this.playlist.findIndex(s => s.collectionId === collectionId)
  if (songIndex > -1) {
    this.playlist.splice(songIndex, 1)
    this.savePlayList()
  }
}

calling this function will look like:

<a target="_blank"><button type="button" class="btn btn-sm btn-danger"
      (click)="remove(user.collectionId)">

remove(collectionId: number) {
  this.list.deleteSongFromPlaylist(collectionId);
}

Upvotes: 1

Related Questions