Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
Cómo usar una cadena delimitada por puntos como ruta de objeto para la directiva vue v-model

tengo registro de datos

 record: { address: { city: "" } }

matriz de objetos que describen el campo

 fields: [ { name: "address.city" ... } ]

y tratar de generar formulario

 <b-field v-for="field in fields" :key="field.name" :label="field.label" > <b-input v-model="record[field.name]" /> </b-field>

y obtenga el elemento del objeto con una clave como address.name . nombre. Entiendo que debo pasar al record[address][city] pero ¿cómo hacerlo desde una cadena delimitada por puntos?

¿Es posible?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

 const getPathInObject = (path, object) => { const [current, ...rest] = path.split('.') return rest.length === 0 ? object[current] : getPathInObject(rest.join('.'), object[current]) } document.write(getPathInObject('abcd', { a: { b: { c: { d: 'hello' } } } }))
EDITAR tal vez algo como esto. aunque muy raro

 const unflatten = obj => { const result = {}; Object.entries(obj).forEach(([key, value]) => { const [a, b] = key.split('.'); result[a] ||= {}; result[a][b] = value }) return result } new Vue({ el: "#app", data: { record_flat: {"address.city": '', "address.country": ''}, fields: [ { name: "address.city", label: 'city' }, { name: "address.country", label: 'country' } ] }, computed: { record: ({ record_flat }) => unflatten(record_flat), } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-for="{name, label} in fields" :key="name"> <label>{{ label }}</label><input v-model="record_flat[name]" /> <br> </div> <hr>Value of record = {{ record }} </div>

over 4 years ago · Santiago Trujillo Report

0

Usé :value y @input en lugar v-model . Y lodash get y set

 <b-input :value="get(data, field.name)" @input="set(data, field.name, $event)" />
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!