Incorporate various inputs into a nested array

I am interested in structuring an array of information. Each piece of information includes a title object with translations in 3 different languages, along with multiple values that are also multilingual.

While referencing this question, I successfully implemented the ability to add multiple pieces of information with their respective titles and initial values. However, I am encountering difficulties when trying to add additional values to each information entry. Specifically, my struggle lies with the addValue() function. I intend to insert a new value object within informations[infIndex].values, but I am unsure about the correct approach. When using push() or slice(), I receive an error stating

informations[infIndex].values.slice is not a function
.

HTML

<div class="informations">
      <h1>Informations</h1>
      <div v-for="(information, infIndex) in informations" :key="infIndex">
        <p>Title</p>
        <input v-model="information.title.en" placeholder="EN" />
        <input v-model="information.title.fr" placeholder="FR" />
        <input v-model="information.title.de" placeholder="DE" />

        <div
          v-for="(value, valIndex) in informations[infIndex].values"
          :key="valIndex"
        >
          <p>Values</p>
          <input v-model="value.en" placeholder="EN" />
          <input v-model="value.fr" placeholder="FR" />
          <input v-model="value.de" placeholder="DE" />
          <button @click="addValue(infIndex, valIndex)">Add Value</button>
        </div>
      </div>
      <button @click="addInformation()">Add Information</button>
      <pre>{{ informations }}</pre>
    </div>

Script setup

const informations = reactive([
  {
    title: {
      en: '',
      fr: '',
      de: '',
    },
    values: {
      value: {
        en: '',
        fr: '',
        de: '',
      },
    },
  },
])

function addInformation() {
  informations.push({
    title: {
      en: '',
      fr: '',
      de: '',
    },
    values: {
      value: {
        en: '',
        fr: '',
        de: '',
      },
    },
  })
}

function addValue(infIndex, valIndex) {
  informations[infIndex].values.splice(valIndex, 0, {
    value: {
      en: '',
      fr: '',
      de: '',
    },
  })
}

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

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

Answer №1

It seems like you are treating informations[infIndex].values as an array when it is actually declared as an object:

values: {
      value: {
        en: '',
        fr: '',
        de: '',
      },
    },

The use of valIndex may be misleading as it is not really an index number but an object key, due to the way v-for with an object is being called:

<div
  v-for="(value, valIndex) in informations[infIndex].values"
  :key="valIndex"
>

To simplify this, consider changing values to an array initially and removing the value key so that it becomes a simple array of objects:

const informations = reactive([
  {
    title: {
      en: '',
      fr: '',
      de: ''
    },
    values: [
      {
        en: '',
        fr: '',
        de: ''
      }
    ]
  }
]);

function addInformation() {
  informations.push({
    title: {
      en: '',
      fr: '',
      de: ''
    },
    values: [
      {
        en: '',
        fr: '',
        de: ''
      }
    ]
  });
}

function addValue(infIndex) {
  informations[infIndex].values.push({
    en: '',
    fr: '',
    de: ''
  });
}

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

regex: sequences containing a dot in the center

Struggling with a regex challenge - need to validate two words with a mandatory point between them. No special characters, no @ symbol, and no spaces allowed. The format should be like this: test.test I've been attempting to use the following regex ...

Converting Blob to File in Electron: A step-by-step guide

Is there a way to convert a Blob into a File object in ElectronJS? I attempted the following: return new File([blob], fileName, {lastModified: new Date().getTime(), type: blob.type}); However, it appears that ElectronJs handles the File object differently ...

Guide to incorporating a scroll-follow effect in multiple directions

I am facing a challenge with managing an array of divs that exceed the dimensions of their container. I have set overflow to hidden on the container and used JQuery Overscroll to achieve an iPhone-like scrolling effect on the map. The problem I'm try ...

Utilize a Random Color Generator to Match Background Color with Border Color in Full Calendar

I'm currently developing a full calendar to showcase a clear and aesthetically pleasing schedule for my client. I am attempting to assign a unique color to each event by generating random colors on every page load using codes. However, I have encounte ...

Encountering a Vue.js error: receiving a 401 Unauthorized response from a Django

Encountering Vue Js error (401 Unauthorized) View Vue Js Error: 401 Image Tools Utilized- Django Rest Framework Vue.js Upon calling the DRF api in Vue.js (using axios), I am facing issues retrieving data. Refer to the code snippet in App.vue below: e ...

What is the best method for transferring the value of a useState variable between separate components in React?

I am working on two components, editor.js and toolbar.js. My goal is to pass the activeTool value from Toolbar.js to editor.js so it can be updated after each click. Editor.js import Toolbar from './Toolbar.js' export default function Editor() ...

inject the HTML content into the <div> container

Snippet: https://jsfiddle.net/x9ghz1ko/ (Includes notes.) In my HTML file, there are two distinct sections: <section class="desktop_only"> and <section class="mobile_only">. The challenge lies in positioning a JavaScript sc ...

What could be causing the JSF ajax status success to fail in Chrome?

Whenever I trigger an action in my ManagedBean, I also initiate a JavaScript function via JSF ajax to display a loading popup while the action is being processed. This functionality works flawlessly in Firefox, however, it seems to encounter issues in Chro ...

Enhance the appearance of your custom three.js Geometry by incorporating different textures

I have been struggling to implement UV-mapping on my custom geometry in three.js. Despite trying various solutions, none seem to be working. Can someone provide a clear explanation of how UV-mapping functions and the correct way to implement it? var s = 1 ...

The inspection of Microsoft VS Code and Angular 2 tags within an HTML code validator

Recently, I began delving into Angular 2 within VSCode. However, I encountered an issue with the built-in HTML linter not recognizing Angular 2 directives like *ng-if or (click) when working on the "Tour of Heroes" project from Angular.io. Here is a glimps ...

Tips for refreshing Iframe content periodically without causing any flickering

Despite placing the Iframe within an update panel, it continues to flicker each time it reload. I am considering using JavaScript to load the Iframe content in order to eliminate this issue. Is this a viable solution? If so, how can I implement it successf ...

Unraveling the Mysteries of Linguistic Evolution

I am curious about how to retrieve data from a map. I have three buttons on a JSP page: Register, Update, and Delete. The JSP files I am working with are First.jsp and Second.jsp. I have included First.jsp within Second.jsp using aaa. The buttons are l ...

The Instant Search feature falls one keystroke short

I’ve implemented a unique "translation" feature on my website which includes an instant search functionality powered by a textarea element. <textarea name="text" onkeydown="searchq();"></textarea> This textarea triggers the execution of the ...

Tips for showcasing overflowing text in a menu list by rotating the item text

Imagine you have a TextMenuItem component, using MenuItem from the Material-UI library, that is part of a chain consisting of DropDownSearch > SimpleListMenu > FixedSizeList > TextMenuItem. In simple terms, this creates a searchable dropdown eleme ...

"The program developed in Php/Ajax/jQuery/Javascript functions flawlessly on one server but encounters challenges on a different host. The mystery unrav

element, I'm encountering an issue that doesn't seem to be related to any specific code problem. There are no error messages indicating what steps I should take next. Here's the problem: I have a script similar to Facebook's wall feat ...

Encountering an issue with Angular routing: Cross-origin requests are restricted to specific protocol schemes, such as HTTP

I encountered an error while working on angular routing. Below is my HTML code snippet: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <script src= ...

Choosing various li classes within a navigation bar

Struggling to pick the right JQuery elements for my portfolio site. The aim is to show/hide the .inner (Task) items by clicking on the .outer (Category) items, complete with rotating .arrows when the menu expands. Check out a similar question, along with ...

VueJS Component has trouble refreshing the DOM content following an AJAX Promise

I've encountered issues updating data in my Vue application after an AJAX callback. I have previously resolved similar problems by using Vue.set, but for some reason, it's not working for me today. The only difference is that I am calling a serv ...

Managing the Response from an Ajax Call

Currently, I am in the process of developing a user registration form for my website and have implemented ajax to manage server-side processes. My main issue lies in effectively handling the response generated by my PHP code. The potential responses from t ...

Create a deep array by combining data from various axios requests in a Vue.js application

Recently delving into Vuejs, I'm grappling with creating a nested array from 2 separate axios requests. I suspect there must be a more elegant solution to accomplish this task. Currently, I am generating two distinct arrays: one for productList (conta ...