Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push both key and value into an Array in Jquery

I am reading RSS feed and pushing both Title and Link into an Array in Jquery.

What i did is

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});                 });                                   }); 

And i am reading that array again using

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

But it Giving me Output as

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

like this ...

How i can get both tile and link as a pair ( title as key and link as value)

like image 734
KillerFish Avatar asked Jan 28 '11 07:01

KillerFish


People also ask

How do you push a key and value in an array?

Answer: Use the Square Bracket [] Syntax php // Sample array $array = array("a" => "Apple", "b" => "Ball", "c" => "Cat"); // Adding key-value pairs to an array $array["d"] = "Dog"; $array["e"] = "Elephant"; print_r($array); ?>

Can array have key value pairs?

Arrays in javascript are typically used only with numeric, auto incremented keys, but javascript objects can hold named key value pairs, functions and even other objects as well.

What is push and pop in array?

push() is used to add an element/item to the end of an array. The pop() function is used to delete the last element/item of the array. Let's try to add and remove elements from an array using push() and pop() methods to understand these methods better.


2 Answers

There are no keys in JavaScript arrays. Use objects for that purpose.

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 ); }); 

In JavaScript, objects fulfill the role of associative arrays. Be aware that objects do not have a defined "sort order" when iterating them (see below).

However, In your case it is not really clear to me why you transfer data from the original object (data.news) at all. Why do you not simply pass a reference to that object around?


You can combine objects and arrays to achieve predictable iteration and key/value behavior:

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 ); }); 
like image 68
Tomalak Avatar answered Sep 25 '22 05:09

Tomalak


This code

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

is not doing what you think it does. What gets pushed is a new object with a single member named "title" and with link as the value ... the actual title value is not used. To save an object with two fields you have to do something like

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

or with recent Javascript advances you can use the shortcut

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

If instead you want the key of the object to be the content of the variable title you can use

arr.push({[title]: link}); // Note that title has been wrapped in brackets 
like image 32
6502 Avatar answered Sep 22 '22 05:09

6502