Jump to content
dzhax

JavaScript or jQuery - Filter a json array of objects

Recommended Posts

Hello All,

 

I am stuck trying to filter results of a json response.

 

I have a sample json response:

[
  {
    "id":0,
    "commentText":"Test",
    "createdBy":"User1",
    "isImportant":false
  },
  {
    "id":1,
    "commentText":"Hello World",
    "createdBy":"User2",
    "isImportant":true
  },
  {
    "id":2,
    "commentText":"Testing",
    "createdBy":"User2",
    "isImportant":false
  },
  {
    "id":3,
    "commentText":"This is another one",
    "createdBy":"User2",
    "isImportant":true
  },
  {
    "id":4,
    "commentText":"Hello World!",
    "createdBy":"User2",
    "isImportant":true
  }
]

I am attempting to filter out results that "isImportant" are not true.

 

I have tried multiple ways of doing it with no luck.

 

IE.

var response = $(jsonSample).filter(function (i,n){
  return n.isImportant===true
});

var response2 = $.grep(Object(jsonSample), function(j){ 
  return j.isImportant !== false; 
});

var response3 = $.map(jsonSample, function (n,i){
  $.each(n, function(j){
    return n.isImportant !== false;
  })
});

console.log(response); //jquery-2.2.4.min.js:2 Uncaught Error: Syntax error, unrecognized expression: ...

console.log(response2); //logs every individual character in the jsonSample ["[", "{", """, "i", "d", """, ":", "1", ...

console.log(response3); //jquery-2.2.4.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in ...
Edited by dzhax

Share this post


Link to post
Share on other sites

You filter the data whose result is assigned to 'response' variable, the you loop through filtered data

           var jsonSample = [
                {
                    "id": 0,
                    "commentText": "Test",
                    "createdBy": "User1",
                    "isImportant": false
                },
                {
                    "id": 1,
                    "commentText": "Hello World",
                    "createdBy": "User2",
                    "isImportant": true
                },
                {
                    "id": 2,
                    "commentText": "Testing",
                    "createdBy": "User2",
                    "isImportant": false
                },
                {
                    "id": 3,
                    "commentText": "This is another one",
                    "createdBy": "User2",
                    "isImportant": true
                },
                {
                    "id": 4,
                    "commentText": "Hello World!",
                    "createdBy": "User2",
                    "isImportant": true
                }
            ];
response = $(jsonSample).filter(function(i, n)
            {
                return   n.isImportant !== false;
            });

            $.each(response, function(i) {
                alert(response[i].commentText);
            });

This is what you mean? yes!

Edited by dsonesuk

Share this post


Link to post
Share on other sites

What I want to do is rebuild the jsonSample filtering out the isImportant false. So what is returned is:

[
  {
    "id":1,
    "commentText":"Hello World",
    "createdBy":"User2",
    "isImportant":true
  },
  {
    "id":3,
    "commentText":"This is another one",
    "createdBy":"User2",
    "isImportant":true
  },
  {
    "id":4,
    "commentText":"Hello World!",
    "createdBy":"User2",
    "isImportant":true
  }
]
Edited by dzhax

Share this post


Link to post
Share on other sites

But, that is what is returned? you just have to reference them by index to see them

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
        <script type="text/javascript">
            var jsonSample = [
                {
                    "id": 0,
                    "commentText": "Test",
                    "createdBy": "User1",
                    "isImportant": false
                },
                {
                    "id": 1,
                    "commentText": "Hello World",
                    "createdBy": "User2",
                    "isImportant": true
                },
                {
                    "id": 2,
                    "commentText": "Testing",
                    "createdBy": "User2",
                    "isImportant": false
                },
                {
                    "id": 3,
                    "commentText": "This is another one",
                    "createdBy": "User2",
                    "isImportant": true
                },
                {
                    "id": 4,
                    "commentText": "Hello World!",
                    "createdBy": "User2",
                    "isImportant": true
                }
            ];


        </script>
        <style type="text/css">

        </style>
    </head>
    <body>
        <div id="result"></div>
        <script>
            var result = "<H3>Current json values (id and comment text are shown only for this example)</h3>";

            $.each(jsonSample, function(i, v) {

                result += jsonSample[i].id + " " + jsonSample[i].commentText + " <br>";

                $('#result').html(result);

            });

            response = $(jsonSample).filter(function(i, n)
            {
                return   n.isImportant !== false;
            });
            result += "<hr>";
            result += "<H3>Filtered response json values (id and comment text are shown only for this example)</h3>";
            $.each(response, function(i, v) {

                result += response[i].id + " " + response[i].commentText + " <br>";

                $('#result').html(result);

            });

            result += "<hr>";
            result += "<H3>jsonSample made to equal response json values (id and comment text are shown only for this example)</h3>";
            jsonSample = response;

            $.each(jsonSample, function(i, v) {

                result += jsonSample[i].id + " " + jsonSample[i].commentText + " <br>";

                $('#result').html(result);

            });


        </script>
    </body>
</html>

Share this post


Link to post
Share on other sites

I think I am getting somewhere but the filter is not actually working

var tmp, tmpJSON, response;
var staticJSON = JSON.stringify(
[{
	"id": 35,
	"commentText": "Hello World.",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-07-29T18:36:39.044Z",
	"updatedAt": "2016-07-29T18:36:39.044Z"
}, {
	"id": 36,
	"commentText": "This is all starting to come together",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:13:54.538Z",
	"updatedAt": "2016-07-29T19:13:54.538Z"
}, {
	"id": 37,
	"commentText": "I'm pretending to be Lou",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-07-29T19:14:41.130Z",
	"updatedAt": "2016-07-29T19:14:41.130Z"
}, {
	"id": 41,
	"commentText": "testing 123",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:21:35.637Z",
	"updatedAt": "2016-07-29T19:21:35.637Z"
}, {
	"id": 42,
	"commentText": "testing123456",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:22:42.516Z",
	"updatedAt": "2016-07-29T19:22:42.516Z"
}, {
	"id": 44,
	"commentText": "This is not important",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:33:54.805Z",
	"updatedAt": "2016-07-29T19:33:54.805Z"
}, {
	"id": 51,
	"commentText": "New functionality",
	"createdBy": "Lou",
	"isImportant": true,
	"createdAt": "2016-08-03T14:38:54.724Z",
	"updatedAt": "2016-08-11T21:16:05.261Z"
}, {
	"id": 92,
	"commentText": "Another test",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-08-11T22:18:06.022Z",
	"updatedAt": "2016-08-11T22:18:06.022Z"
}, {
	"id": 29,
	"commentText": "hot garbage 2.0",
	"createdBy": "Kevin",
	"isImportant": true,
	"createdAt": "2016-07-28T21:59:35.145Z",
	"updatedAt": "2016-08-04T21:41:16.361Z"
}, {
	"id": 54,
	"commentText": "please work",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-04T23:24:02.682Z",
	"updatedAt": "2016-08-04T23:24:02.682Z"
}, {
	"id": 93,
	"commentText": "test comment",
	"createdBy": "mark",
	"isImportant": false,
	"createdAt": "2016-08-11T22:32:00.067Z",
	"updatedAt": "2016-08-11T22:32:00.067Z"
}, {
	"id": 78,
	"commentText": "Testing modal dialog",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-08-10T01:22:10.015Z",
	"updatedAt": "2016-08-10T01:22:10.015Z"
}, {
	"id": 94,
	"commentText": "test comment",
	"createdBy": "mark",
	"isImportant": true,
	"createdAt": "2016-08-11T22:32:24.534Z",
	"updatedAt": "2016-08-11T22:32:24.534Z"
}, {
	"id": 61,
	"commentText": "wooo",
	"createdBy": "yes",
	"isImportant": false,
	"createdAt": "2016-08-09T22:04:42.671Z",
	"updatedAt": "2016-08-11T15:51:51.448Z"
}, {
	"id": 81,
	"commentText": "Deleted files.  Just checking if it still works",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-08-11T19:26:36.869Z",
	"updatedAt": "2016-08-11T19:26:36.869Z"
}, {
	"id": 83,
	"commentText": "Trying it again",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-08-11T20:29:41.439Z",
	"updatedAt": "2016-08-11T20:29:41.439Z"
}, {
	"id": 107,
	"commentText": "New!",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-11T23:03:50.597Z",
	"updatedAt": "2016-08-11T23:03:50.597Z"
}, {
	"id": 112,
	"commentText": "One more time",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-11T23:23:49.398Z",
	"updatedAt": "2016-08-11T23:23:49.398Z"
}, {
	"id": 115,
	"commentText": "Try this",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-11T23:32:54.382Z",
	"updatedAt": "2016-08-11T23:32:54.382Z"
}]
      );
      console.log(staticJSON);
      console.log(" ");
      console.log(" ");
      console.log(" ");
      console.log(" ");
      tmpJson = $(JSON.parse(staticJSON)).filter(function (i, n){
        return n.isimportant !== false;
      });
      tmp = "[";
      $.each(tmpJson, function(i) {
        tmp += '{';
        tmp += '"commentText": "' + tmpJson[i].commentText + '", ';
        tmp += '"createdAt": "' + tmpJson[i].createdAt + '", ';
        tmp += '"createdBy": "' + tmpJson[i].createdBy + '", ';
        tmp += '"id": ' + tmpJson[i].id + ', ';
        tmp += '"isImportant": ' + tmpJson[i].isImportant + ', ';
        tmp += '"updatedAt": "' + tmpJson[i].updatedAt + '"';
        tmp += '},';
      });
      tmp = tmp.substring(0, tmp.length - 1);
      tmp += ']';
      console.log(tmp);
      response = tmp;
Sorry for changing up the array but this is live code I am trying to work with.

 

Console Log

[{
	"id": 35,
	"commentText": "Hello World.",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-07-29T18:36:39.044Z",
	"updatedAt": "2016-07-29T18:36:39.044Z"
}, {
	"id": 36,
	"commentText": "This is all starting to come together",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:13:54.538Z",
	"updatedAt": "2016-07-29T19:13:54.538Z"
}, {
	"id": 37,
	"commentText": "I'm pretending to be Lou",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-07-29T19:14:41.130Z",
	"updatedAt": "2016-07-29T19:14:41.130Z"
}, {
	"id": 41,
	"commentText": "testing 123",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:21:35.637Z",
	"updatedAt": "2016-07-29T19:21:35.637Z"
}, {
	"id": 42,
	"commentText": "testing123456",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:22:42.516Z",
	"updatedAt": "2016-07-29T19:22:42.516Z"
}, {
	"id": 44,
	"commentText": "This is not important",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-07-29T19:33:54.805Z",
	"updatedAt": "2016-07-29T19:33:54.805Z"
}, {
	"id": 51,
	"commentText": "New functionality",
	"createdBy": "Lou",
	"isImportant": true,
	"createdAt": "2016-08-03T14:38:54.724Z",
	"updatedAt": "2016-08-11T21:16:05.261Z"
}, {
	"id": 92,
	"commentText": "Another test",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-08-11T22:18:06.022Z",
	"updatedAt": "2016-08-11T22:18:06.022Z"
}, {
	"id": 29,
	"commentText": "hot garbage 2.0",
	"createdBy": "Kevin",
	"isImportant": true,
	"createdAt": "2016-07-28T21:59:35.145Z",
	"updatedAt": "2016-08-04T21:41:16.361Z"
}, {
	"id": 54,
	"commentText": "please work",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-04T23:24:02.682Z",
	"updatedAt": "2016-08-04T23:24:02.682Z"
}, {
	"id": 93,
	"commentText": "test comment",
	"createdBy": "mark",
	"isImportant": false,
	"createdAt": "2016-08-11T22:32:00.067Z",
	"updatedAt": "2016-08-11T22:32:00.067Z"
}, {
	"id": 78,
	"commentText": "Testing modal dialog",
	"createdBy": "Lou",
	"isImportant": false,
	"createdAt": "2016-08-10T01:22:10.015Z",
	"updatedAt": "2016-08-10T01:22:10.015Z"
}, {
	"id": 94,
	"commentText": "test comment",
	"createdBy": "mark",
	"isImportant": true,
	"createdAt": "2016-08-11T22:32:24.534Z",
	"updatedAt": "2016-08-11T22:32:24.534Z"
}, {
	"id": 61,
	"commentText": "wooo",
	"createdBy": "yes",
	"isImportant": false,
	"createdAt": "2016-08-09T22:04:42.671Z",
	"updatedAt": "2016-08-11T15:51:51.448Z"
}, {
	"id": 81,
	"commentText": "Deleted files.  Just checking if it still works",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-08-11T19:26:36.869Z",
	"updatedAt": "2016-08-11T19:26:36.869Z"
}, {
	"id": 83,
	"commentText": "Trying it again",
	"createdBy": "Mark",
	"isImportant": false,
	"createdAt": "2016-08-11T20:29:41.439Z",
	"updatedAt": "2016-08-11T20:29:41.439Z"
}, {
	"id": 107,
	"commentText": "New!",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-11T23:03:50.597Z",
	"updatedAt": "2016-08-11T23:03:50.597Z"
}, {
	"id": 112,
	"commentText": "One more time",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-11T23:23:49.398Z",
	"updatedAt": "2016-08-11T23:23:49.398Z"
}, {
	"id": 115,
	"commentText": "Try this",
	"createdBy": "Dana",
	"isImportant": false,
	"createdAt": "2016-08-11T23:32:54.382Z",
	"updatedAt": "2016-08-11T23:32:54.382Z"
}]




[{
	"commentText": "Hello World.",
	"createdAt": "2016-07-29T18:36:39.044Z",
	"createdBy": "Lou",
	"id": 35,
	"isImportant": false,
	"updatedAt": "2016-07-29T18:36:39.044Z"
}, {
	"commentText": "This is all starting to come together",
	"createdAt": "2016-07-29T19:13:54.538Z",
	"createdBy": "Mark",
	"id": 36,
	"isImportant": false,
	"updatedAt": "2016-07-29T19:13:54.538Z"
}, {
	"commentText": "I'm pretending to be Lou",
	"createdAt": "2016-07-29T19:14:41.130Z",
	"createdBy": "Lou",
	"id": 37,
	"isImportant": false,
	"updatedAt": "2016-07-29T19:14:41.130Z"
}, {
	"commentText": "testing 123",
	"createdAt": "2016-07-29T19:21:35.637Z",
	"createdBy": "Mark",
	"id": 41,
	"isImportant": false,
	"updatedAt": "2016-07-29T19:21:35.637Z"
}, {
	"commentText": "testing123456",
	"createdAt": "2016-07-29T19:22:42.516Z",
	"createdBy": "Mark",
	"id": 42,
	"isImportant": false,
	"updatedAt": "2016-07-29T19:22:42.516Z"
}, {
	"commentText": "This is not important",
	"createdAt": "2016-07-29T19:33:54.805Z",
	"createdBy": "Mark",
	"id": 44,
	"isImportant": false,
	"updatedAt": "2016-07-29T19:33:54.805Z"
}, {
	"commentText": "New functionality",
	"createdAt": "2016-08-03T14:38:54.724Z",
	"createdBy": "Lou",
	"id": 51,
	"isImportant": true,
	"updatedAt": "2016-08-11T21:16:05.261Z"
}, {
	"commentText": "Another test",
	"createdAt": "2016-08-11T22:18:06.022Z",
	"createdBy": "Lou",
	"id": 92,
	"isImportant": false,
	"updatedAt": "2016-08-11T22:18:06.022Z"
}, {
	"commentText": "hot garbage 2.0",
	"createdAt": "2016-07-28T21:59:35.145Z",
	"createdBy": "Kevin",
	"id": 29,
	"isImportant": true,
	"updatedAt": "2016-08-04T21:41:16.361Z"
}, {
	"commentText": "please work",
	"createdAt": "2016-08-04T23:24:02.682Z",
	"createdBy": "Dana",
	"id": 54,
	"isImportant": false,
	"updatedAt": "2016-08-04T23:24:02.682Z"
}, {
	"commentText": "test comment",
	"createdAt": "2016-08-11T22:32:00.067Z",
	"createdBy": "mark",
	"id": 93,
	"isImportant": false,
	"updatedAt": "2016-08-11T22:32:00.067Z"
}, {
	"commentText": "Testing modal dialog",
	"createdAt": "2016-08-10T01:22:10.015Z",
	"createdBy": "Lou",
	"id": 78,
	"isImportant": false,
	"updatedAt": "2016-08-10T01:22:10.015Z"
}, {
	"commentText": "test comment",
	"createdAt": "2016-08-11T22:32:24.534Z",
	"createdBy": "mark",
	"id": 94,
	"isImportant": true,
	"updatedAt": "2016-08-11T22:32:24.534Z"
}, {
	"commentText": "wooo",
	"createdAt": "2016-08-09T22:04:42.671Z",
	"createdBy": "yes",
	"id": 61,
	"isImportant": false,
	"updatedAt": "2016-08-11T15:51:51.448Z"
}, {
	"commentText": "Deleted files.  Just checking if it still works",
	"createdAt": "2016-08-11T19:26:36.869Z",
	"createdBy": "Mark",
	"id": 81,
	"isImportant": false,
	"updatedAt": "2016-08-11T19:26:36.869Z"
}, {
	"commentText": "Trying it again",
	"createdAt": "2016-08-11T20:29:41.439Z",
	"createdBy": "Mark",
	"id": 83,
	"isImportant": false,
	"updatedAt": "2016-08-11T20:29:41.439Z"
}, {
	"commentText": "New!",
	"createdAt": "2016-08-11T23:03:50.597Z",
	"createdBy": "Dana",
	"id": 107,
	"isImportant": false,
	"updatedAt": "2016-08-11T23:03:50.597Z"
}, {
	"commentText": "One more time",
	"createdAt": "2016-08-11T23:23:49.398Z",
	"createdBy": "Dana",
	"id": 112,
	"isImportant": false,
	"updatedAt": "2016-08-11T23:23:49.398Z"
}, {
	"commentText": "Try this",
	"createdAt": "2016-08-11T23:32:54.382Z",
	"createdBy": "Dana",
	"id": 115,
	"isImportant": false,
	"updatedAt": "2016-08-11T23:32:54.382Z"
}]
Edited by dzhax

Share this post


Link to post
Share on other sites

IF you still wish to JSON.stringify() you have to destringify with JSON.parse()

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
        <script type="text/javascript">

            var jsonSample = JSON.stringify(
                    [{
                            "id": 35,
                            "commentText": "Hello World.",
                            "createdBy": "Lou",
                            "isImportant": false,
                            "createdAt": "2016-07-29T18:36:39.044Z",
                            "updatedAt": "2016-07-29T18:36:39.044Z"
                        }, {
                            "id": 36,
                            "commentText": "This is all starting to come together",
                            "createdBy": "Mark",
                            "isImportant": false,
                            "createdAt": "2016-07-29T19:13:54.538Z",
                            "updatedAt": "2016-07-29T19:13:54.538Z"
                        }, {
                            "id": 37,
                            "commentText": "I'm pretending to be Lou",
                            "createdBy": "Lou",
                            "isImportant": false,
                            "createdAt": "2016-07-29T19:14:41.130Z",
                            "updatedAt": "2016-07-29T19:14:41.130Z"
                        }, {
                            "id": 41,
                            "commentText": "testing 123",
                            "createdBy": "Mark",
                            "isImportant": false,
                            "createdAt": "2016-07-29T19:21:35.637Z",
                            "updatedAt": "2016-07-29T19:21:35.637Z"
                        }, {
                            "id": 42,
                            "commentText": "testing123456",
                            "createdBy": "Mark",
                            "isImportant": false,
                            "createdAt": "2016-07-29T19:22:42.516Z",
                            "updatedAt": "2016-07-29T19:22:42.516Z"
                        }, {
                            "id": 44,
                            "commentText": "This is not important",
                            "createdBy": "Mark",
                            "isImportant": false,
                            "createdAt": "2016-07-29T19:33:54.805Z",
                            "updatedAt": "2016-07-29T19:33:54.805Z"
                        }, {
                            "id": 51,
                            "commentText": "New functionality",
                            "createdBy": "Lou",
                            "isImportant": true,
                            "createdAt": "2016-08-03T14:38:54.724Z",
                            "updatedAt": "2016-08-11T21:16:05.261Z"
                        }, {
                            "id": 92,
                            "commentText": "Another test",
                            "createdBy": "Lou",
                            "isImportant": false,
                            "createdAt": "2016-08-11T22:18:06.022Z",
                            "updatedAt": "2016-08-11T22:18:06.022Z"
                        }, {
                            "id": 29,
                            "commentText": "hot garbage 2.0",
                            "createdBy": "Kevin",
                            "isImportant": true,
                            "createdAt": "2016-07-28T21:59:35.145Z",
                            "updatedAt": "2016-08-04T21:41:16.361Z"
                        }, {
                            "id": 54,
                            "commentText": "please work",
                            "createdBy": "Dana",
                            "isImportant": false,
                            "createdAt": "2016-08-04T23:24:02.682Z",
                            "updatedAt": "2016-08-04T23:24:02.682Z"
                        }, {
                            "id": 93,
                            "commentText": "test comment",
                            "createdBy": "mark",
                            "isImportant": false,
                            "createdAt": "2016-08-11T22:32:00.067Z",
                            "updatedAt": "2016-08-11T22:32:00.067Z"
                        }, {
                            "id": 78,
                            "commentText": "Testing modal dialog",
                            "createdBy": "Lou",
                            "isImportant": false,
                            "createdAt": "2016-08-10T01:22:10.015Z",
                            "updatedAt": "2016-08-10T01:22:10.015Z"
                        }, {
                            "id": 94,
                            "commentText": "test comment",
                            "createdBy": "mark",
                            "isImportant": true,
                            "createdAt": "2016-08-11T22:32:24.534Z",
                            "updatedAt": "2016-08-11T22:32:24.534Z"
                        }, {
                            "id": 61,
                            "commentText": "wooo",
                            "createdBy": "yes",
                            "isImportant": false,
                            "createdAt": "2016-08-09T22:04:42.671Z",
                            "updatedAt": "2016-08-11T15:51:51.448Z"
                        }, {
                            "id": 81,
                            "commentText": "Deleted files. Just checking if it still works",
                            "createdBy": "Mark",
                            "isImportant": false,
                            "createdAt": "2016-08-11T19:26:36.869Z",
                            "updatedAt": "2016-08-11T19:26:36.869Z"
                        }, {
                            "id": 83,
                            "commentText": "Trying it again",
                            "createdBy": "Mark",
                            "isImportant": false,
                            "createdAt": "2016-08-11T20:29:41.439Z",
                            "updatedAt": "2016-08-11T20:29:41.439Z"
                        }, {
                            "id": 107,
                            "commentText": "New!",
                            "createdBy": "Dana",
                            "isImportant": false,
                            "createdAt": "2016-08-11T23:03:50.597Z",
                            "updatedAt": "2016-08-11T23:03:50.597Z"
                        }, {
                            "id": 112,
                            "commentText": "One more time",
                            "createdBy": "Dana",
                            "isImportant": false,
                            "createdAt": "2016-08-11T23:23:49.398Z",
                            "updatedAt": "2016-08-11T23:23:49.398Z"
                        }, {
                            "id": 115,
                            "commentText": "Try this",
                            "createdBy": "Dana",
                            "isImportant": false,
                            "createdAt": "2016-08-11T23:32:54.382Z",
                            "updatedAt": "2016-08-11T23:32:54.382Z"
                        }]
                    );



        </script>
        <style type="text/css">

        </style>
    </head>
    <body>
        <div id="result"></div>
        <script>
            var result = "<H3>Current json values (id and comment text are shown only for this example)</h3>";
            var destringify = JSON.parse(jsonSample);
            $.each(destringify, function(i, v) {

                result += destringify[i].id + " " + destringify[i].commentText + " <br>";

                $('#result').html(result);

            });

            response = $(destringify).filter(function(i, n)
            {
                return   n.isImportant !== false;
            });
            result += "<hr>";
            result += "<H3>Filtered response json values (id and comment text are shown only for this example)</h3>";
            $.each(response, function(i, v) {

                result += response[i].id + " " + response[i].commentText + " <br>";

                $('#result').html(result);

            });

            result += "<hr>";
            result += "<H3>destringify made to equal response json values (id and comment text are shown only for this example)</h3>";
            destringify = response;

            $.each(destringify, function(i, v) {

                result += destringify[i].id + " " + destringify[i].commentText + " <br>";

                $('#result').html(result);

            });


        </script>
    </body>
</html>

Share this post


Link to post
Share on other sites

i am destringifying it

 

tmpJson = $(JSON.parse(staticJSON)).filter(function (i, n){

Share this post


Link to post
Share on other sites

It's not really necessary to use jQuery for that, and it would probably even be faster without the overhead.

 

var filtered = [];
for (var i = 0; i < data.length; i++) {
  if (data[i].isImportant) {
    filtered.push(data[i]);
  }
}

Share this post


Link to post
Share on other sites

Ok I got it working.
after changing tmpJSON to response.
and parsing the JSON outside of the filter

var destringify = JSON.parse(staticJSON);
$.each(destringify, function(i, v) {
 

rather than

$(JSON.parse(staticJSON)).filter(function (i, n){

Not sure why this was a thing but it works and that's all that matters right now.

Edited by dzhax

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...