filter and append AJAX JSON result based on objects with a specific key value











up vote
0
down vote

favorite












I'm trying to display the 3 records that have the value "sort":"list:"



I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!



$.ajax({
type: 'GET',
url: '/stores',
success: function(stores) {
// if (stores.sort === 'list') {
$.each(stores, function(i, store) {
// if (stores.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
// }
});
//}
}
});


My JSON is pretty simple:



[
{
"_id": "5be78df2fb6fc06239e0c39b",
"name": "Albertsons",
"sort": "list"
},
{
"_id": "5be78e00fb6fc06239e0c39c",
"name": "COSTCO",
"sort": "list"
},
{
"_id": "5be78e17fb6fc06239e0c3ac",
"name": "Food Lion",
"sort": "bank"
},
{
"_id": "5be78e34fb6fc06239e0c3b1",
"name": "7Eleven",
"sort": "list"
},
{
"_id": "5be78e5ffb6fc06239e0c3b7",
"name": "Kroger",
"sort": "bank"
}
]









share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to display the 3 records that have the value "sort":"list:"



    I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!



    $.ajax({
    type: 'GET',
    url: '/stores',
    success: function(stores) {
    // if (stores.sort === 'list') {
    $.each(stores, function(i, store) {
    // if (stores.sort === 'list'){
    $stores.append(`<div><img src="${store.name}.jpg"></div>`);
    // }
    });
    //}
    }
    });


    My JSON is pretty simple:



    [
    {
    "_id": "5be78df2fb6fc06239e0c39b",
    "name": "Albertsons",
    "sort": "list"
    },
    {
    "_id": "5be78e00fb6fc06239e0c39c",
    "name": "COSTCO",
    "sort": "list"
    },
    {
    "_id": "5be78e17fb6fc06239e0c3ac",
    "name": "Food Lion",
    "sort": "bank"
    },
    {
    "_id": "5be78e34fb6fc06239e0c3b1",
    "name": "7Eleven",
    "sort": "list"
    },
    {
    "_id": "5be78e5ffb6fc06239e0c3b7",
    "name": "Kroger",
    "sort": "bank"
    }
    ]









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to display the 3 records that have the value "sort":"list:"



      I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!



      $.ajax({
      type: 'GET',
      url: '/stores',
      success: function(stores) {
      // if (stores.sort === 'list') {
      $.each(stores, function(i, store) {
      // if (stores.sort === 'list'){
      $stores.append(`<div><img src="${store.name}.jpg"></div>`);
      // }
      });
      //}
      }
      });


      My JSON is pretty simple:



      [
      {
      "_id": "5be78df2fb6fc06239e0c39b",
      "name": "Albertsons",
      "sort": "list"
      },
      {
      "_id": "5be78e00fb6fc06239e0c39c",
      "name": "COSTCO",
      "sort": "list"
      },
      {
      "_id": "5be78e17fb6fc06239e0c3ac",
      "name": "Food Lion",
      "sort": "bank"
      },
      {
      "_id": "5be78e34fb6fc06239e0c3b1",
      "name": "7Eleven",
      "sort": "list"
      },
      {
      "_id": "5be78e5ffb6fc06239e0c3b7",
      "name": "Kroger",
      "sort": "bank"
      }
      ]









      share|improve this question













      I'm trying to display the 3 records that have the value "sort":"list:"



      I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!



      $.ajax({
      type: 'GET',
      url: '/stores',
      success: function(stores) {
      // if (stores.sort === 'list') {
      $.each(stores, function(i, store) {
      // if (stores.sort === 'list'){
      $stores.append(`<div><img src="${store.name}.jpg"></div>`);
      // }
      });
      //}
      }
      });


      My JSON is pretty simple:



      [
      {
      "_id": "5be78df2fb6fc06239e0c39b",
      "name": "Albertsons",
      "sort": "list"
      },
      {
      "_id": "5be78e00fb6fc06239e0c39c",
      "name": "COSTCO",
      "sort": "list"
      },
      {
      "_id": "5be78e17fb6fc06239e0c3ac",
      "name": "Food Lion",
      "sort": "bank"
      },
      {
      "_id": "5be78e34fb6fc06239e0c3b1",
      "name": "7Eleven",
      "sort": "list"
      },
      {
      "_id": "5be78e5ffb6fc06239e0c3b7",
      "name": "Kroger",
      "sort": "bank"
      }
      ]






      jquery json ajax






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 2:56









      Casey

      416




      416
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          stores is the entire response, which is an array, not an individual object you're iterating over, so stores.sort won't be meaningful. Check the store property instead:



          success: function(stores) {
          $.each(stores, function(i, store) {
          if (store.sort === 'list'){
          $stores.append(`<div><img src="${store.name}.jpg"></div>`);
          }
          });
          }


          Or, you might filter beforehand instead:



          success: function(stores) {
          const listStores = stores.filter(({ sort }) => sort === 'list');
          listStores.forEach(({ name }) => {
          $stores.append(`<div><img src="${name}.jpg"></div>`);
          });
          }





          share|improve this answer





















          • Perfect Thank You!
            – Casey
            Nov 15 at 1:32


















          up vote
          0
          down vote













          You can use $.grep to filter out the objects you want.






          $(document).ready(function(){
          var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
          var jsonObject = JSON.parse(jsonString);

          // Filter your object using grep
          var filteredObjects = $.grep(jsonObject, function(a){
          return a.sort == "list"
          });

          var elements = '';
          for(var i = 0; i<filteredObjects.length; i++ ){
          elements += '<div>'+filteredObjects[i].name+'</div>'
          }
          $('#container').append(elements);
          })

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

          <div id="container"></div>








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245456%2ffilter-and-append-ajax-json-result-based-on-objects-with-a-specific-key-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            stores is the entire response, which is an array, not an individual object you're iterating over, so stores.sort won't be meaningful. Check the store property instead:



            success: function(stores) {
            $.each(stores, function(i, store) {
            if (store.sort === 'list'){
            $stores.append(`<div><img src="${store.name}.jpg"></div>`);
            }
            });
            }


            Or, you might filter beforehand instead:



            success: function(stores) {
            const listStores = stores.filter(({ sort }) => sort === 'list');
            listStores.forEach(({ name }) => {
            $stores.append(`<div><img src="${name}.jpg"></div>`);
            });
            }





            share|improve this answer





















            • Perfect Thank You!
              – Casey
              Nov 15 at 1:32















            up vote
            1
            down vote



            accepted










            stores is the entire response, which is an array, not an individual object you're iterating over, so stores.sort won't be meaningful. Check the store property instead:



            success: function(stores) {
            $.each(stores, function(i, store) {
            if (store.sort === 'list'){
            $stores.append(`<div><img src="${store.name}.jpg"></div>`);
            }
            });
            }


            Or, you might filter beforehand instead:



            success: function(stores) {
            const listStores = stores.filter(({ sort }) => sort === 'list');
            listStores.forEach(({ name }) => {
            $stores.append(`<div><img src="${name}.jpg"></div>`);
            });
            }





            share|improve this answer





















            • Perfect Thank You!
              – Casey
              Nov 15 at 1:32













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            stores is the entire response, which is an array, not an individual object you're iterating over, so stores.sort won't be meaningful. Check the store property instead:



            success: function(stores) {
            $.each(stores, function(i, store) {
            if (store.sort === 'list'){
            $stores.append(`<div><img src="${store.name}.jpg"></div>`);
            }
            });
            }


            Or, you might filter beforehand instead:



            success: function(stores) {
            const listStores = stores.filter(({ sort }) => sort === 'list');
            listStores.forEach(({ name }) => {
            $stores.append(`<div><img src="${name}.jpg"></div>`);
            });
            }





            share|improve this answer












            stores is the entire response, which is an array, not an individual object you're iterating over, so stores.sort won't be meaningful. Check the store property instead:



            success: function(stores) {
            $.each(stores, function(i, store) {
            if (store.sort === 'list'){
            $stores.append(`<div><img src="${store.name}.jpg"></div>`);
            }
            });
            }


            Or, you might filter beforehand instead:



            success: function(stores) {
            const listStores = stores.filter(({ sort }) => sort === 'list');
            listStores.forEach(({ name }) => {
            $stores.append(`<div><img src="${name}.jpg"></div>`);
            });
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 3:01









            CertainPerformance

            66.6k143152




            66.6k143152












            • Perfect Thank You!
              – Casey
              Nov 15 at 1:32


















            • Perfect Thank You!
              – Casey
              Nov 15 at 1:32
















            Perfect Thank You!
            – Casey
            Nov 15 at 1:32




            Perfect Thank You!
            – Casey
            Nov 15 at 1:32












            up vote
            0
            down vote













            You can use $.grep to filter out the objects you want.






            $(document).ready(function(){
            var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
            var jsonObject = JSON.parse(jsonString);

            // Filter your object using grep
            var filteredObjects = $.grep(jsonObject, function(a){
            return a.sort == "list"
            });

            var elements = '';
            for(var i = 0; i<filteredObjects.length; i++ ){
            elements += '<div>'+filteredObjects[i].name+'</div>'
            }
            $('#container').append(elements);
            })

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

            <div id="container"></div>








            share|improve this answer

























              up vote
              0
              down vote













              You can use $.grep to filter out the objects you want.






              $(document).ready(function(){
              var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
              var jsonObject = JSON.parse(jsonString);

              // Filter your object using grep
              var filteredObjects = $.grep(jsonObject, function(a){
              return a.sort == "list"
              });

              var elements = '';
              for(var i = 0; i<filteredObjects.length; i++ ){
              elements += '<div>'+filteredObjects[i].name+'</div>'
              }
              $('#container').append(elements);
              })

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

              <div id="container"></div>








              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can use $.grep to filter out the objects you want.






                $(document).ready(function(){
                var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
                var jsonObject = JSON.parse(jsonString);

                // Filter your object using grep
                var filteredObjects = $.grep(jsonObject, function(a){
                return a.sort == "list"
                });

                var elements = '';
                for(var i = 0; i<filteredObjects.length; i++ ){
                elements += '<div>'+filteredObjects[i].name+'</div>'
                }
                $('#container').append(elements);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

                <div id="container"></div>








                share|improve this answer












                You can use $.grep to filter out the objects you want.






                $(document).ready(function(){
                var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
                var jsonObject = JSON.parse(jsonString);

                // Filter your object using grep
                var filteredObjects = $.grep(jsonObject, function(a){
                return a.sort == "list"
                });

                var elements = '';
                for(var i = 0; i<filteredObjects.length; i++ ){
                elements += '<div>'+filteredObjects[i].name+'</div>'
                }
                $('#container').append(elements);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

                <div id="container"></div>








                $(document).ready(function(){
                var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
                var jsonObject = JSON.parse(jsonString);

                // Filter your object using grep
                var filteredObjects = $.grep(jsonObject, function(a){
                return a.sort == "list"
                });

                var elements = '';
                for(var i = 0; i<filteredObjects.length; i++ ){
                elements += '<div>'+filteredObjects[i].name+'</div>'
                }
                $('#container').append(elements);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

                <div id="container"></div>





                $(document).ready(function(){
                var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
                var jsonObject = JSON.parse(jsonString);

                // Filter your object using grep
                var filteredObjects = $.grep(jsonObject, function(a){
                return a.sort == "list"
                });

                var elements = '';
                for(var i = 0; i<filteredObjects.length; i++ ){
                elements += '<div>'+filteredObjects[i].name+'</div>'
                }
                $('#container').append(elements);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

                <div id="container"></div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 3:18









                SilentCoder

                1,84211020




                1,84211020






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245456%2ffilter-and-append-ajax-json-result-based-on-objects-with-a-specific-key-value%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python