What's preventing the dynamic update of nested arrays in my specific situation?

Hello, I seem to be having an issue with updating a nested array dynamically. You can see the problem illustrated in the image below:

https://i.sstatic.net/IgkJ9.png

Below are the steps to reproduce the problem:

  1. Click on the Add Likes button to add likes for example New Apple
  2. By default, my like is set to Mango
  3. Click on the Add about button to add the next row (Mango does not appear), then click on Add Likes to observe the issue.

To enhance code readability, I have placed helper functions in a mixin file named mixin.js

Expectation: I should be able to add any number of Add about and Add Likes (with the default value being Mango)

Here is my code snippet: https://codesandbox.io/s/musing-hawking-di4d3?file=/src/App.vue

Answer №1

Initially, there is no need for a nested array for the tags property. Instead, use:

getLikesTemplate() {
  let year = this.year;
  let template = {
    id: this.getUniqueId(),
    like: `I'm ${year} Old and like things like`,
    tags: [this.getTagsTemplate("Mango")]          //removed nesting
  };
  this.year++;
  return template;
}

Additionally, in JavaScript, objects are passed by reference, allowing you to do this:

method:

addLikes(like) {                                   //extra code removed
  like.tags.push(this.getTagsTemplate("New Apple"));
},

template:

... 
<div style="text-align: left; display: flex">
  <div>                               //nested structure removed
    <div class="tags" v-for="tag in like.tags" :key="tag.id">    
      {{ tag.name }}
    </div>
  </div>                              //object reference passed
  <button style="margin-left: 20px" @click="addLikes(like)">
    Add Likes
  </button>
</div>

Image of result

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

Seamlessly adaptive HTML5 video playback

Has anyone figured out how to make an HTML5 video in an absolutely positioned <video> element resize to fit the window width and height without ever getting cropped? Many solutions I've come across rely on using the <iframe> tag, which is ...

Can we streamline this jQuery code by utilizing .hasClass and .bind?

Can this code be simplified? Initially, when #griffyindor has the 'active' class, I want all other houses (slytherin, ravenclaw, and hufflepuff) to show. If at any point it loses the 'active' class upon clicking something else, I want ...

Vue: Implementing Data Refresh in Store Post-Login

I am encountering an issue with the data store "menus" not updating after a login. It seems that the object "loggedInUser" is not set before calling "getMenus". I'm unsure of what mistake I might be making here. PS! While debugging in Chrome, I notic ...

The issue related to a form control named 'No value accessor' lacking an accessor

As I work on a stackblitz for an issue with child-parent communication, a different problem has arisen. Specifically, I am encountering the error message: No value accessor for form control with name: 'endDateFC'. The same error is occurring for ...

Starting a segment of an array in Java

Currently studying java and after checking the documentation, I couldn't find any information on this topic. Is it feasible to initialize only a portion of an array with various strings all in one line like this: String[] array = new String[20] {"wat ...

Working with streams in Node.js by utilizing piping with both AJAX and the HTTP module

Running an express app with AJAX calls to remote endpoints has been a smooth process so far. However, I'm facing a unique challenge with one particular POST method that requires a query parameter named code and a body of type Array[int]. Surprisingly, ...

Missing out on the opportunity to operate on a GET request

After running the code displayed in the attached screenshot, I observed the following behavior: the FOR loop is executed first, followed by two 'LET' statements (let path_get... and let get = https.get...). Issue: when the second LET statement i ...

Encountering a JavaScript toJSON custom method causing a StackOverflow error

Unique Scenario Upon discovering this answer, a new idea struck me - creating an object from a JSON literal. This led me to believe I could do the opposite using the handy JSON method: JSON.stringify(myObject). Curious, I proceeded as follows: function ...

What causes a component to not update when it is connected to an Array using v-model?

Array1 https://i.stack.imgur.com/cY0XR.jpg Array are both connected to the same Array variable using v-model. However, when changes are made to Array1, Array2 does not update. Why is this happening? Process: Upon examining the logs, it can be observed th ...

Tips for implementing the onload function in Rails view

I would like to execute a function once the form has finished loading. Here is my controller containing the function that receives the AJAX: def get_component_from_work @work = Work.find(params[:work_id]) @<a href="/cdn-cgi/l/email-protection" cla ...

Ways to display tinyMCE content in a unique manner

I've been diving into node.js/express/mongoDB and decided to create a blog. I encountered an issue with rendering the content inputted through tinyMCE as HTML - instead, it's displaying the tags around my content. How can I properly display it as ...

What is the best way to append items onto my 2D ArrayList?

Currently, I am in the process of developing an inventory program, and I believe that utilizing a two-dimensional ArrayList would be beneficial. For example, I could assign an item code of "001" to the first array index [0], and then store the item name, d ...

Is it possible to include ternary in the "homepage" section of the package.json file?

When making API calls in production, they will be on the same domain as where my app is hosted, so it's necessary to include "homepage": "." in the package.json. However, when working locally from localhost, I need to make API calls to the production ...

I created a new game where I can select two images to display, but for some reason, it is only showing one

My rock paper scissors game functions well, except for one issue. I programmed it to display both the player's choice and the bot's choice along with an outcome message (win, tie, or lose). However, I encountered a problem where only the player&a ...

What is the best way to tally values or obtain a summary in an array iteration?

The data dd($arr) I'm working with is similar to the image below: https://i.sstatic.net/raZU3.png Is there a way to calculate the sum of the retweet_count values, for example 233 + 2 = 235? ...

Issue with ng-show not toggling visibility on ng-click event in AngularJS

When I click on an Edit Mode link, I want to display some Edit icons. I've tried using ng-click with ng-class and ng-show but it's not working as expected. Here is the HTML code: <div click-to-edit="questions.n1.name" class="row question"> ...

Adjusting the size and style of the <textarea > dynamically

Is it possible to dynamically change the size of a field based on user input? I have a select field with 3 options, each with different values. When a user selects an option, I want the value to fit within the field size. How can I adjust the size of ...

fuel ux checkbox mark all

I am having trouble implementing a feature where all checkboxes are selected when clicking on a specific checkbox. <div class="checkbox" id="myCheckbox2"> <label class="checkbox-custom" data-initialize="checkbox"> <input class="sr-onl ...

Set Sprite Canvas Material to match the color of a different element

Can someone please guide me on changing the color of the Sprite Canvas Material? var material = new THREE.SpriteCanvasMaterial( { color: 0xffffff, I am trying to make it match the color of another element based on a specific ID or class. However, I' ...

Accessing Elasticsearch from Kibana without the need for authentication and sending requests freely

Currently, I am in the process of developing a plugin for Kibana with the intention of establishing communication with Elasticsearch, utilizing Shield for security measures. Thus far, my approach has involved sending requests through the server with code ...