Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
how to v-model on different array vue 3

Hi I was trying to use a v-model an input to a value in object in an array of object in Vue 3. The complexity lies in the fact the object is first processed by a function. And that it need to be processed every time when a change is made to an input. Here is my code (and a sandbox link) :

<template>
  <div id="app">
    <div v-for="param in process(parameters)" :key="param">
    Name :   {{param.name}} Value : <input v-model="param.value">
    </div>
    {{parameters}}
  </div>
</template>
<script>


export default {
  name: "App",
  data(){
    return{
    parameters :[
      {'name':'Richard Stallman','value':'cool dude'},
      {'name':'Linus Torvalds','value':'very cool dude'},
      {'name':'Dennis Ritchie','value':'very very cool dude'}
      ]
    }
  },
  methods:{
    process(parameters){
      const results = parameters.map( param =>{
        return {name:param.name+' extra text',
                value:param.value+' extra text',
                }
      })
      return results
    }
  }
};
</script>
I just want the original parameters to change when something is types in the inputs. Maybe @change could be of use. But I didn't find a fix with @change. Does anyone know a solution to my problem? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I am not entirely sure I understood whether the person should be able to see/edit the text you added within you processing method.

Anyway, I think this sample of code should solve you problem :

<template>
  <div id="app">
    <div v-for="param in parameters" :key="param.name">
      Name : {{ param.name }} Value : <input v-model="param.value" />
    </div>
    {{ process }}
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      parameters: [
        { name: "Richard Stallman", value: "cool dude" },
        { name: "Linus Torvalds", value: "very cool dude" },
        { name: "Dennis Ritchie", value: "very very cool dude" },
      ],
    };
  },
  computed: {
    process: function() {
      const results = this.parameters.map((param) => {
        return {
          name: param.name + " extra text",
          value: param.value + " extra text",
        };
      });
      return results;
    },
  },
};
</script>

So, we're iterating through the parameters array directly, adding an input on the value just like you did. When you type in the input, you update the parameter linked to it, in live.

I just switched the method you made into a computed method. This way, every time parameters is updated, "process" is also updated because it's depending on it directly.

I also removed passing the "parameters" argument, it's in the component data, you can just access it directly.

This way, using "process" just like any variable, you'll always have the updated parameters + what you added to em.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use computed property to get reactive state of the data.

Working Demo :

new Vue({
  el: '#app',
  data: {
    parameters :[
      {'name':'Richard Stallman','value':'cool dude'},
      {'name':'Linus Torvalds','value':'very cool dude'},
      {'name':'Dennis Ritchie','value':'very very cool dude'}
    ]
  },
  computed: {
    process() {
      const results = this.parameters.map((param) => {
        return {
            name: param.name + ' extra text',
          value: param.value + ' extra text'
        }
      });
      return results;
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="param in process" :key="param">
    Name : {{param.name}}
    Value : <input v-model="param.value">
  </div><br>
  <strong>Orinigal Data :</strong> {{parameters}}
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda