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)
You might mean this:
You can set an enumerable for an Object like:
({})[0] = 'txt';
and you can set a key for an Array like:([])['myKey'] = 'myVal';
Hope this helps :)
There are no keys in JavaScript arrays. Use objects for that purpose.
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:
You're not pushing into the array, you're setting the element with the key
title
to the valuelink
. As such your array should be an object.I think you need to define an object and then push in array
This code
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 actualtitle
value is not used. To save an object with two fields you have to do something likeor with recent Javascript advances you can use the shortcut
The closest thing to a python tuple would instead be
Once you have your objects or arrays in the
arr
array you can get the values either asvalue.title
andvalue.link
or, in case of the pushed array version, asvalue[0]
,value[1]
.