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

199
Views
¿Cómo crear matrices múltiples a partir de una sola matriz?

Recibo datos de formulario en mi consola de esta manera:

 {item[0][sku]: 'EC1000-WMK', item[0][qty]: '1', item[1][sku]: 'POP-11', item[1][qty]: '1', form_key: 'ZuQxqBMHmidjUxEt'} form_key: "ZuQxqBMHmidjUxEt" item[0][qty]: "1" item[0][sku]: "EC1000-WMK" item[1][qty]: "1" item[1][sku]: "POP-11"

Así es como se crearon los datos anteriores:

 $.fn.getFormData = function(){ var data = {}; var dataArray = $(this).serializeArray(); console.log(data); console.log(dataArray); for(var i=0;i<dataArray.length;i++){ console.log(dataArray); data[dataArray[i].name] = dataArray[i].value; } return data; }

pero quiero hacerlo como:

 [ { "sku": "EC1000-WMK", "qty": "1" } { "sku": "POP-11", "qty": "1" } ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use el nombre de los campos de entrada como

 <input name="item.0.sku" /> <input name="item.0.qty" /> <input name="item.1.sku" /> <input name="item.2.qty" />

& luego use esta función Aquí está la función que convierte todas las entradas de formulario al formato JSON

 const jsonSerialize = ($this) => { $this = $this.find('input, select'); let data = {}; for (let i = 0; i < $this.length; i++) { let el = $this[i]; let val = el.value; if (!val) val = ""; let fullName = el.getAttribute("name"); if (!fullName) continue; let fullNameParts = fullName.split('.'); let prefix = ''; let stack = data; for (let k = 0; k < fullNameParts.length - 1; k++) { prefix = fullNameParts[k]; if (!stack[prefix]) { stack[prefix] = {}; } stack = stack[prefix]; } prefix = fullNameParts[fullNameParts.length - 1]; if (stack[prefix]) { let newVal = stack[prefix] + ',' + val; stack[prefix] += newVal; } else { stack[prefix] = val; } } return data } let $form = $('form'); params = jsonSerialize($form)

Espero que esto funcione para ti

about 4 years ago · Juan Pablo Isaza 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!