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

后端 未结 5 2279
[愿得一人]
[愿得一人] 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:08
    arr[title] = link;
    

    You're not pushing into the array, you're setting the element with the key title to the value link. As such your array should be an object.

    0 讨论(0)
  • 2020-12-12 12:10

    You might mean this:

    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 an enumerable for an Object like: ({})[0] = 'txt'; and you can set a key for an Array like: ([])['myKey'] = 'myVal';

    Hope this helps :)

    0 讨论(0)
  • 2020-12-12 12:17

    I think you need to define an object and then push in array

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

    0 讨论(0)
  • 2020-12-12 12:28

    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 );
    });
    
    0 讨论(0)
提交回复
热议问题