How to push both key and value to an array in jQuery

I am reading an 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 read this array again using

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

But it gives me a way out

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

like this...

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

+54
jquery arrays key-value
Jan 28 2018-11-11T00:
source share
5 answers

JavaScript arrays have no keys. Use objects for this 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 act as associative arrays. Keep in mind that objects do not have a specific โ€œsort orderโ€ when iterating over them (see below).

However . In your case, itโ€™s not entirely clear to me why you generally transfer data from the original object ( data.news ). Why aren't you just passing a link to this object?




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 ); }); 
+113
Jan 28 '11 at 7:38
source share

This code

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

Don't do what you think. What gets the push is a new object with one member named "title" and link as the value ... the actual value of the title not used. To save an object with two fields, you need to do something like

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

The closest thing to python tuple will be

 arr.push([title, link]); 

When you have your objects or arrays in the arr array, you can get the values โ€‹โ€‹either as value.title and value.link , or, in the case of the version of the array, as value[0] , value[1] .

+18
Jan 28 2018-11-11T00:
source share

I think you need to define an object and then click an array

 var obj = {}; obj[name] = val; ary.push(obj); 
+10
Oct 28 '15 at 22:41
source share
 arr[title] = link; 

You do not push into the array, you set the element with the title key to the link value. So your array should be an object.

+7
Jan 28 '11 at 7:38
source share

This may mean the following:

 var unEnumeratedArray = []; var wtfObject = { key : 'val', 0 : (undefined = 'Look, I\'m defined'), 'new' : 'keyword', '{!}' : 'use bracket syntax', ' ': '8 spaces' }; for(var key in wtfObject){ unEnumeratedArray[key] = wtfObject[key]; } console.log('HAS KEYS PER VALUE NOW:', unEnumeratedArray, unEnumeratedArray[0], unEnumeratedArray.key, unEnumeratedArray['new'], unEnumeratedArray['{!}'], unEnumeratedArray[' ']); 

You can set the enumerated for an object as: ({})[0] = 'txt'; , and you can set the key for an array of type: ([])['myKey'] = 'myVal';

Hope this helps :)

+2
04 Oct '13 at
source share



All Articles