Comparison of arrays or best method to add new elements

0

I have a webservice in json that returns me to the last people who passed in a certain place.

That said, I did the following:

  • I always ask for information about the last person to go to the api.

  • I created in my page in setInterval 1s to read this api and make a prepend with the information of this json on the screen within a div

I have noticed that there is a problem. When several people pass in less than 1s I lose or better I skip the display of some people on the screen.

I set the api default to always return the last 9 people so I need to compare the box ids that are already on the screen with the ids that come from json and know which ids do not have on the screen so I can include them in prepend. Can anyone help me with this question of what ids exist in an array 1 that do not exist in array 2?

I made a code according to the help below but it is returning vasio

setInterval(function() {    
    var api = [],tela = [];
    $(".cardface").each(function(){
        tela.push($(this).attr('idimg'));
    });

    $.getJSON('<?php url(); ?>/rtffeed', function(data) {
        $.each(data, function(key,val){
            api.push(val.id_imagens);
        });        
    });


    var adicionar = $(api).not(tela).get();

    console.log(adicionar);

}, 1000);

and if I do

console.log(tela);

it returns {317,318} and I do

console.log(api);

it returns {322,321,319,318,317,316,315,314,313}

Then the add should be {322,321,319,316,315,314,313}

But it is coming back blank

screen, api, add in this order

    
asked by anonymous 29.08.2018 / 15:36

1 answer

1

You can do this if you like, by testing if one element does not exist in the other:

var json = [9,8,7,6,5,4,3,2,1];           // elementos à adicionar
var tela = [4,2];                         // elementos já adicionados

var adicionar = $(json).not(tela).get();  // retorna os que não derão match

console.log(String(adicionar));           // Resultado em forma de String
console.log(adicionar);                   // Resultado em forma de Array
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
29.08.2018 / 16:17