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

259
Views
Cómo insertar tanto la clave como el valor en una matriz en Jquery

Estoy leyendo el feed RSS y presionando Título y Enlace en una matriz en Jquery .

lo que hice es

 var arr = []; $.getJSON("displayjson.php",function(data){ $.each(data.news, function(i,news){ var title = news.title; var link = news.link; arr.push({title : link}); }); });

Y estoy leyendo esa matriz nuevamente usando

 $('#show').click(function(){ $.each(arr, function(index, value){ alert( index +' : '+value); }); });

Pero me da salida como

 1:[Object Object] 2:[Object Object] 3:[Object Object]

Me gusta esto ...

Cómo puedo obtener mosaico y enlace como un par ( título como clave y enlace como valor )

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

No hay claves en las matrices de JavaScript. Utilizar objetos para tal fin.

 var obj = {}; $.getJSON("displayjson.php",function (data) { $.each(data.news, function (i, news) { obj[news.title] = news.link; }); }); // later: $.each(obj, function (index, value) { alert( index + ' : ' + value ); });

En JavaScript, los objetos cumplen la función de matrices asociativas. Tenga en cuenta que los objetos no tienen un "orden de clasificación" definido al iterarlos (ver más abajo).

Sin embargo , en su caso, no me queda muy claro por qué transfiere datos del objeto original ( data.news ). ¿Por qué no pasa simplemente una referencia a ese objeto?


Puede combinar objetos y matrices para lograr una iteración predecible y un comportamiento clave/valor:

 var arr = []; $.getJSON("displayjson.php",function (data) { $.each(data.news, function (i, news) { arr.push({ title: news.title, link: news.link }); }); }); // later: $.each(arr, function (index, value) { alert( value.title + ' : ' + value.link ); });
over 4 years ago · Santiago Trujillo Report

0

este codigo

 var title = news.title; var link = news.link; arr.push({title : link});

no está haciendo lo que crees que hace. Lo que se empuja es un nuevo objeto con un solo miembro llamado "título" y con link como valor... el valor del title real no se usa. Para guardar un objeto con dos campos tienes que hacer algo como

 arr.push({title:title, link:link});

o con los avances recientes de Javascript, puede usar el acceso directo

 arr.push({title, link}); // Note: comma "," and not colon ":"

Si, en cambio, desea que la clave del objeto sea el contenido del title de la variable, puede usar

 arr.push({[title]: link}); // Note that title has been wrapped in brackets
over 4 years ago · Santiago Trujillo Report

0

Creo que necesitas definir un objeto y luego insertarlo en la matriz

 var obj = {}; obj[name] = val; ary.push(obj);
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!