1
我的应用程序是使用Pubnub,我检索的聊天记录与下面的代码:比较数组用foreach项目
$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);
而且我想他们与检索到的新的消息进行比较上面的populateMessages函数。如果timetoken已经存在什么也不做,如果timetoken不存在添加消息的消息($ scope.messages.push(消息);)
http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array –