Integrate fresh data into an array using Vue

Exploring VUE for the first time, I am working on a project where I need to update an array by passing an object. The scenario involves two buttons named BUTTON 1 and BUTTON 2. Clicking on BUTTON 1 sets an object in the list[] using this.$set. However, when BUTTON 2 is clicked, it should update with a new value and display as:

{
 a:1,
 b:2,
 c:3
}

Currently, I am using this.$set for button 2 as well, which replaces the previous value with the new one like {c:3} only. Is there a way in VUE to add on the value so that it displays {a:1, b:2, c:3} when BUTTON 2 is clicked?

View

<div id="app">
  <button @click="button1()">Button 1</button>
  <button @click="button2()">Button 2</button>
</div>

Method

new Vue({
  el: "#app",
  data: {
    list:[]
  },
  methods: {
    
    button1(){
      var b = '0';
      this.$set(this.list, b, {
        1:{
            a: '1',
            b: '2'
        }
      })
      console.log(this.list);
    },
    
    button2(){
      var b = '0';
      this.$set(this.list, b,{
        1:{
            c: '3'
        }
      })
      console.log(this.list);
    },
    
  }
})

Above code can be accessed via the following jsfiddle link:

https://jsfiddle.net/ujjumaki/enj3dwo6/19/

Answer №1

Let's hope this solution does the trick!

new Vue({
el: "#app",
data: {
items:[]
 },
 methods {

clickButton(){
console.log('Clicked button');

const index = '0';
const remainingData=this.items[index] && this.items[index][1]
    this.$set(this.items, index, {
    1:{
    ...remainingData,
        x: '4',
        y: '8'
    }
  })
  
  console.log(this.items);
},

pressButton(){

const index = '0';
const remainingData=this.items[index] && this.items[index][1]
    this.$set(this.items, index,{
    1:{
    ...remainingData,
        z: '12'
    }
  })
  
  console.log(this.items);
},

}
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing ESM-Enabled Script in Forever

Is it possible to execute a script using the option -r esm in Forever? When I try to run the configuration below, I encounter an error message that reads: Unexpected token import. Here is the configuration: { "uid": "app", "script": "index.js", "s ...

`I'm encountering issues when trying to pass an array through localStorage into a new array`

This is a complex and detailed question that I am struggling to find a solution for. Despite using deprecated mysql due to hosting limitations, the problem lies elsewhere. Part 1 involves dataLoader.php, which queries the database and retrieves posx and p ...

VueJS - vue2-editor rejecting certain tags

I have successfully integrated the vue2-editor, and here is the code snippet: <div id="app"> <vue-editor v-model="content"></vue-editor> <div v-html="content"></div> </div> </t ...

How can I reduce the burden of dependencies on users with a pre-built NPM package?

I recently took over maintenance of an NPM package. It has a unique setup where the main file is located in the 'dist/' directory and it's built using webpack (via 'npm run build'). While this works for us, installing this package ...

What is the process for removing a document attribute in Sanity iO?

I have a collection of objects within my Sanity Document named Images which includes Comments An example comment object in the comments array looks like: { "_key": "6510dc79cf8b", "comment": "Hello world" ...

Is there a way to sort through objects in a JSON file using two shared values? Specifically, I'm looking to filter the JSON objects based on both common x and y values

Given a JSON file, I am looking to group objects based on common x and y values. Essentially, I want to group together objects that share the same x and y properties. Here is an example of the JSON data: let data = [{ "x": "0", "y& ...

Implement two different styles within an element's style properties

Welcome everyone, I have a question regarding adding marquee effects to a table row along with highlighting it. Is it possible to include moving text from left to right in the same active row? For example, let's say that the current time is 12:45 AM. ...

Having trouble getting the if statement to run with JavaScript Closures?

Check out this code snippet: let url = URL; let imageURL = ''; $.getJSON("https://graph.facebook.com/?id="+encodeURIComponent(url)+"&scrape=true&method=post", function (data) { let json_data = JSON.stringify(data); json_data ...

Trouble with Global Variable Allocation in jQuery Ajax

I've got a script that includes a jQuery Ajax call: <script type="text/javascript"> $(document).ready(function() { var timer; $.ajax({ type: 'POST', url: 'closettime.php', success: function( res ) ...

The timing calculations in Vue.js do not align with the standard JavaScript version

I am currently working on developing a 'beats per minute' (BPM) calculator, which is similar to the one available here. However, I have noticed that when using the BPM calculator from that link for testing on a particular song, it quickly approxi ...

What is the method for dynamically invoking computed props in templates?

Here is a similar situation: <script> export default { computed: { example () { return 'bar' } } } </script> <template> <div> <!-- Unfortunately, attempting to call "this" ...

Tips for pulling out particular information from a string?

There is a text document that needs to be parsed. The objective is to extract the strings between "@5c00\n" and "@ffd2\n", as well as between "@ffd2\n" and "@". @5c00 81 00 00 5C B1 13 3E 01 0C 43 B1 13 A6 00 1C 43 B1 13 38 01 32 D0 10 00 ...

Add a click event listener to the body element using a button click, without causing it to trigger

What is the current situation: A button is clicked by the user The menu opens (list items display = block) The function to close the menu is connected to the <body> The function to close the menu is immediately triggered, causing the menu to close ...

Ensure that TypeScript compiled files are set to read-only mode

There is a suggestion on GitHub to implement a feature in tsc that would mark compiled files as readonly. However, it has been deemed not feasible and will not be pursued. As someone who tends to accidentally modify compiled files instead of the source fil ...

Updating multiple documents in Mongoose that have a specific element in an array

I have structured a Mongoose schema like this: const mongoose = require('mongoose'); const ItemSchema = mongoose.Schema({ id: { type: String, required: true, }, items: { type: Array, required: true, }, date: { type: ...

Provide the remaining arguments in a specific callback function in TypeScript while abiding by strict mode regulations

In my code, I have a function A that accepts another function as an argument. Within function A, I aim to run the given function with one specific parameter and the remaining parameters from the given function. Here's an example: function t(g: number, ...

What is an alternative way to show the contents of a JSON file without directly accessing it

Recently, I stumbled upon an amazing website - where I have been exploring to learn something new. The website prominently features the use of Ajax and some fascinating javascript without any additional libraries. Within a single javascript file on this ...

Parsing JSON using dotted paths in Python

Are there any Python libraries available on PyPI that allow me to parse JSON using dot-separated paths and order-independent arrays? 1. dot separated path 2. order independant in case of array Data : "attr1": [ { "attr2": "val2" }, ...

An issue arises when trying to import the modal popup

Hey there! I'm currently trying to implement a modal popup in react-bootstrap, but I keep running into an error that says "Props not defined". I followed the instructions from the react-bootstrap documentation, but I can't seem to figure out what ...

No action is triggered after submitting AJAX data in a WordPress environment

I'm currently working on developing a WordPress plugin that requires querying the database. However, I am facing challenges in getting it to function properly. Here is the code snippet that I have attempted: jQuery('#demo_ajax').submit(func ...