Remove whitespace in Array

0

Well, I'm developing an app with ionic 3. As I try to write some data to the array, "\ n" appears at the beginning and at the end of the array. Here is the code:

 element = this.pedido.produto;

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

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

In LocalStorage, it looks like this:

[\n celular \n]
    
asked by anonymous 29.11.2018 / 12:18

1 answer

0

You can use .trim()

element = this.pedido.produto;

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

  localStorage.setItem("vetor", JSON.stringify(this.vetCarrinho.trim()));
    
29.11.2018 / 15:38