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

232
Views
jquery/json: crea una matriz dinámica para la clave

No descubrí cómo crear un mensaje json en jquery con valores dinámicos para un elemento de matriz clave.

Estoy tratando de construir un mensaje json como este:

 { "line": { "name": "Test", "images": [ { "order": "1", "link": "https://google.com/1.jpg", "name": "1.jpg" }, { "order": "2", "link": "https://google.com/2.jpg", "name": "2.jpg" } ] } }

No puedo adjuntar las fotos al mensaje.

 jsonObbj = {}; line = {}; line ["name"] = $("#name").val(); counter = 1; $("div.carousel_image > img").each(function() { image = {}; image ["order"] = counter; image ["name"] = $(this).attr('src'); line ["images"].push(image); // How can I edit this image to the images array counter++; }); // how can I add the line to the array

¡Gracias por tu ayuda!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe inicializar line.images en una matriz vacía.

 jsonObbj = {}; line = {}; line ["name"] = $("#name").val(); line["images"] = []; // add this line counter = 1; $("div.carousel_image > img").each(function() { image = {}; image ["order"] = counter; image ["name"] = $(this).attr('src'); line ["images"].push(image); // How can I edit this image to the images array counter++; });

Tenga en cuenta que, en general, cuando las claves de su objeto son cadenas normales, desea utilizar la notación de puntos: line.images

JSFiddle: https://jsfiddle.net/to4xhewu/

about 4 years ago · Juan Pablo Isaza Report

0

Debe inicializar line.images como una matriz antes de poder comenzar a presionarlo. También debe asegurarse de declarar sus variables correctamente con const , let o var o, de lo contrario, serán implícitamente globales .

Pruebe esto para una alternativa de inicializador literal de objeto simple

 const jsonObbj = { line: { name: $("#name").val(), images: $("div.carousel_image > img").map((i, img) => ({ order: i + 1, name: img.getAttribute("src"), link: img.src })).get() } }

Ver https://api.jquery.com/map/

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!