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

后端 未结 5 2280
[愿得一人]
[愿得一人] 2020-12-12 11:45

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

What i did is

var arr = [];

               


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 12:23

    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 ":"
    

    The closest thing to a python tuple would instead be

    arr.push([title, link]);
    

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

提交回复
热议问题