jQuery.inArray不工作?

时间:2016-09-23 14:13:54

标签: javascript jquery arrays json object

所以我正在使用ajax调用来检索用户的消息。然后通过创建一个id的数组来交叉引用它们与现有消息。我有两条消息已经存在,ID为157和159.两条消息都在数组中正确列出,但是当我使用jQuery.inArray时,它似乎只能找到其中一条消息。

function checkForNewMessages() {
    $CurrentMessages = [];

    $('.Message').each(function () {
        $CurrentMessages.push($(this).attr('message-id'));
    });

    console.log('Messages found');
    console.log($CurrentMessages);

    console.log('Getting Messages');
    $.ajax({url: "<?php echo $GLOBALS['SubDirectory']; ?>api/get/messenger/messagesfromfeed?feed_id=" + urlget('ID'), success: function(result){
        $Result = decodeURI_array(JSON.parse(result));
        $.each($Result, function(index) {
            if(!jQuery.inArray($Result[index]['ID'], $CurrentMessages)) {
                console.log("New Message, ID of '" + $Result[index]['ID'] + "'");
            } else {
                console.log("Old Message, ID of '" + $Result[index]['ID'] + "'");
            }
        });
    }});
}

控制台结果

Messages found
["157", "159"]
Getting Messages
New Message, ID of '157'
Message, ID of '159'

URI解码JSON

[{"ID":"157","CompanyID":"13","Receiver":"13","Sender":"","Message":"test line 1\r\n& test line 2\r\n\r\n","VirtualAttachments":"[{\"Type\":\"Client\",\"ID\":\"128\",\"DisplayName\":\"\"},{\"Type\":\"Quote\",\"ID\":\"20\",\"DisplayName\":\"\"}]","FileAttachments":"[{\"Name\":\"IMG_1300.JPG\",\"Location\":\"\"}]","FeedID":"23","Seen":"TRUE","CreatedOn":"1474635298"},{"ID":"159","CompanyID":"13","Receiver":"13","Sender":"15","Message":"reply test\r\n","VirtualAttachments":"[]","FileAttachments":"[]","FeedID":"23","Seen":"TRUE","CreatedOn":"1474635960"}]

0 个答案:

没有答案