Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140131

remove duplication from jQuery array

$
0
0

I have a page that can have the same article twice or more in it i have an ajax search in this page ,when i do the search with the name of article it shows twice if there is two of it in this page , so i want to remove the duplicate and to display every article one time in the search result . this is the controller

public function searchAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $requestString = $request->get('q');
    $articles =  $em->getRepository('ArticleBundle:Article')->findEntitiesByString($requestString);
    if(!$articles) {
        $result['articles']['error'] = "article Not found :( ";
    } else {
        $result['articles'] = $this->getRealEntities($articles);
    }
    return new Response(json_encode($result));
}
public function getRealEntities($articles){
    foreach ($articles as $articles){
        $realEntities[$articles->getId()] = [$articles->displayCover(),$articles->getTitle()];
    }
    return $realEntities;
}
}

and this is the twig

<script type="text/javascript">
    jQuery(document).ready(function() {
        var searchRequest = null;
        $("#search").keyup(function() {
            var minlength = 1;
            var that = this;
            var value = $(this).val();




            var entitySelector = $("#entitiesNav").html('');
            if (value.length >= minlength ) {


                if (searchRequest != null)
                    searchRequest.abort();
                searchRequest = $.ajax({
                    type: "GET",
                    url: "{{ path('search_ajax') }}",
                    data: {
                        'q' : value
                    },
                    dataType: "text",
                    success: function(msg){


                        //we need to check if the value is the same
                        if (value===$(that).val()) {

                            var result = JSON.parse(msg);
                            $.each(result, function(key, arr) {
                                $.each(arr, function(id, value) {
                                    if (key === 'articles') {
                                        if (id !== 'error') {
                                            console.log(value[1]);

                                            entitySelector.append('<div class="media post-block m-b-xs-30">\n' +
                                                '<div class="media post-block m-b-xs-30"> '+'<a href="/Studentmag/web/app_dev.php/mag/article/'+id+'">'+'<img class=" m-r-xs-30" src="'+value[0]+'">'+'</a>'+'<div class="media-body">'+'<div class="post-cat-group m-b-xs-10">\n' +
                                                '\t\t\t\t\t\t\t\t\t<a href="business.html" class="post-cat cat-btn bg-color-blue-two">SPORTS</a>\n' +
                                                '\t\t\t\t\t\t\t\t</div><h3 class="axil-post-title hover-line hover-line">'+value[1]+'</h3>'+'<div class="post-metas">\n' +
                                                '\t\t\t\t\t\t\t\t\t<ul class="list-inline">\n' +
                                                '\t\t\t\t\t\t\t\t\t\t<li>By <a href="#">Xu Jianhong</a></li>\n' +
                                                '\t\t\t\t\t\t\t\t\t</ul>\n' +
                                                '\t\t\t\t\t\t\t\t</div>\n' +
                                                '\t\t\t\t\t\t\t</div> '+'</div>\n' +
                                                '</div>');
                                        } else {
                                            entitySelector.append('<li class="errorLi">'+value+'</li>');
                                        }
                                    }
                                });
                            });
                        }
                    }
                });
            }

        });

    });
</script>

Viewing all articles
Browse latest Browse all 140131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>