Remove Whitespace in Array

Asked

Viewed 38 times

0

Well, I’m developing an app with Ionic 3. When I try to record some data in the array, it appears " n" at the beginning and end of the array. Follow the code :

 element = this.pedido.produto;

if (this.vetCarrinhoAux.indexOf(element) === -1) {
  this.vetCarrinho.push(element);

  localStorage.setItem("vetor", JSON.stringify(this.vetCarrinho));

At Localstorage, he’s like this :

[\n celular \n]
  • 1

    Try using the . Trim()

  • Thank you so much for your help. It worked.

1 answer

0

You can use the .trim()

element = this.pedido.produto;

if (this.vetCarrinhoAux.indexOf(element) === -1) {
  this.vetCarrinho.push(element);

  localStorage.setItem("vetor", JSON.stringify(this.vetCarrinho.trim()));

Browser other questions tagged

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