Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
In Vue V2, how do I merge two arrays of objects and save them to local storage?

I am trying to make a to-do list app where the list is filled with tasks from an API. I also want to be able to add tasks and have them saved when I refresh. Yet I cann't seem to merge the two arrays, the API and the user inputted tasks. For now, I'd like to save them to my local storage, however, it is not working and I cannot figure it out. Also, I am unable to figure out how to do this in my created function. Could anyone point me to any resources or can immediately see what is wrong? Any help is greatly appreciated!

var app = new Vue({
  el: '#app',
  data () {
    return {
      items: [{
        title: "",
        completed: false,
        }],
        title: '',
        show: 'all',
    }
  },
  mounted () {
    if(localStorage.getItem('items')) {
      try {
        this.items = JSON.parse(localStorage.getItem('items'));
      } catch(e) {
        localStorage.removeItem('items');
      }
    }
  },
  created: function () {
    axios
      .get("https://jsonplaceholder.typicode.com/todos")
      .then(response => this.items = response.data)
    items.concat(items)
  },
  computed: {
    activeItems() {
      this.saveItems;
      return this.items.filter(item => {
        return !item.completed;
      });
    },
    filteredItems() {
      if (this.show === 'active')
        return this.items.filter(item => {
          return !item.completed;
        });
      if (this.show === 'completed')
        return this.items.filter(item => {
          return item.completed;
        });
      return this.items.reverse();
    },
    
  },
  methods: {
    addItem() {
      this.items.push({
        title: this.title,
        completed: false
      });
      this.title = '';
      this.saveItems();
    },
    deleteItem(item) {
      var index = this.items.indexOf(item);
      if (index > -1)
        this.items.splice(index, 1);
      this.saveItems();
    },
    saveItems() {
      const parsed = JSON.stringify(this.item);
      localStorage.setItem('items', parsed);
    },
    showActive() {
      this.show = 'active';
    },
    showAll() {
      this.show = 'all';
    },
    showCompleted() {
      this.show = 'completed';
    },
    deleteCompleted() {
      this.items = this.items.filter(item => {
        return !item.completed;
      });
    },
  }
});

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

please have a look at this article - Creating a Simple To-do App With Vue: List Rendering, And Class Binding

In your case you should use either created or mounted function. No need to use both. If you use both, you should know the correct order in which they are called. However, there are promises and async code. So, I recommend using only one.

You can do something like below

mounted () {
    axios
      .get("https://jsonplaceholder.typicode.com/todos")
      .then(response => {
         const apiItems = response.data;
         if(localStorage.getItem('items')) {
           try {
             this.items = apiItems.concat(JSON.parse(localStorage.getItem('items')));
           } catch(e) {
             localStorage.removeItem('items');
             this.items = apiItems;
           }
         }
       })
  },
about 4 years ago · Juan Pablo Isaza Relatório

0

Step 1: HTML template

<div id="app">
  <ul>
    <li v-for="(item, index) in items" :key="index">{{ item.title }}</li>
  </ul>
  <div>
    <input type="text" v-model="title" />
    <button type="button" @click.prevent="addItem">Add</button>
  </div>
</div>

Step 2: Update your add button action event

addItem() {
  const data = {
    title: this.title,
    completed: false,
  };
  this.items.push(data);
  this.saveItems(data);
  this.clearForm();
},

Step 3: Update your saveItems method

Before adding to localStorage you have to consider below steps,

  • Have to check already have localStorage data

  • If already have localStorage data then you have to push the new values to the old array

  • If no localStorage data then you have to save the new values in an array format

    saveItems(item) {
      var localSaveItems = [];
      var getLocalItems = localStorage.getItem("items");
      if (getLocalItems) {
          getLocalItems = JSON.parse(getLocalItems);
          localSaveItems = [...localSaveItems, ...getLocalItems];
          localSaveItems.push(item);
          localStorage.setItem("items", JSON.stringify(localSaveItems));
      } else {
          localSaveItems.push(item);
          localStorage.setItem("items", JSON.stringify(localSaveItems));
      }
    },
    clearForm() {
       this.title = "";
    },
    

Step 4: Update the create function

created: function () {
  axios.get("https://jsonplaceholder.typicode.com/todos").then((response) => {
    this.items = response.data;
    if (localStorage.getItem("items")) {
      try {
        this.localStorageItems = JSON.parse(localStorage.getItem("items"));
        this.items = [...this.items, ...this.localStorageItems];
      } catch (e) {
        localStorage.removeItem("items");
      }
    }
  });
},

Once loaded the REST API data you have to check already having localStorage data or not. If you have localStorage data then you have join two arrays.

Using Spread operation you can join two arrays like below,

this.items = [...this.items, ...this.localStorageItems];

DEMO

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda