Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数组与forEach项进行比较_Javascript_Angularjs_Pubnub - Fatal编程技术网

Javascript 将数组与forEach项进行比较

Javascript 将数组与forEach项进行比较,javascript,angularjs,pubnub,Javascript,Angularjs,Pubnub,我的应用程序正在使用Pubnub,我正在使用以下代码检索聊天历史记录: $scope.populateMessages = function() { // Populate message history using jsapi (optional) PubNub.jsapi.history({ channel: $scope.channel, count: 15, callba

我的应用程序正在使用Pubnub,我正在使用以下代码检索聊天历史记录:

$scope.populateMessages = function() {       
        // Populate message history using jsapi (optional)
        PubNub.jsapi.history({
            channel: $scope.channel,
            count: 15,
            callback: function(payload) {
                payload[0].forEach(function(message) {
                $scope.messages.push(message);

                //message timetoken
                console.log(message.pn_apns.aps.timetoken);
            });
            $scope.messagesStatus = false;
            $scope.connectionStatus  = false;
            $scope.$apply();
            }
        }); 
    };
当我的应用程序来自后台时,我想再次检查新消息。 但是当我运行上面的代码时,会得到重复的代码。如果我在运行上面的代码之前清除$scope.messages,那么它只会工作,只有加载时间

因此,我当前要做的是通过检索现有消息的时间标记来检查当前消息列表:

var div = $('.list');
var divs = $('.item .chatMessage');
var divArray = [];

for (var i = 0; i < divs.length; i += 1) {
    divArray.push(divs.attr('timetoken'));
}

//list of existing messages timetokens
console.log(divArray);
var div=$('.list');
var divs=$('.item.chatMessage');
var divArray=[];
对于(变量i=0;i

我想将它们与上面的populateMessages函数检索到的新消息进行比较。如果消息timetoken已存在,则不执行任何操作;如果timetoken不存在,则添加消息($scope.messages.push(message);)

如果您正在使用库,如
lodash
下划线
,则可以执行此操作

$scope.messages=..uniq($scope.messages,函数(msg){return msg.timetoken;})