Arrays comparison or best method to add new elements

Asked

Viewed 25 times

0

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

That said I did the following:

  • I only ask for the api always the information of the last person to pass on site.

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

Done this I 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 by default the api to always return the last 9 people to me 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 don’t have on the screen so I can include these people in the prepend. Could someone assist me in this question of knowing which 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

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);

whereas if I do

console.log(tela);

he returns {317,318} and I do

console.log(api);

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

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

but he’s coming back blank

inserir a descrição da imagem aqui screen, api, add in this order

  • But the ids come in array format? Puts the format of the ids return.

  • will ta in array I have already formatted I have 2 arrays one with the list of the ids of the people who already have on the screen and the other with the list of the last 9 people who passed on the site

  • var json = [9,8,7,6,5,4,3,2,1], screen = [4,2];

  • would be so if it were set in the hand

1 answer

1

Pose to do so if you like, 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>

  • give a look I did an update on my post with the code as it was but not working.

  • But vc is giving the console.log(api , screen) where Jasar? Have to give out functions each to see if the arrays are even being filled in.

  • were yes and was giving console.log in the middle of Cod and was returning values as in print

Browser other questions tagged

You are not signed in. Login or sign up in order to post.