locked
How can i delete a number of items from a JSON object RRS feed

  • Question

  • User1253338400 posted

    Hi,

    I have the following json data that is returned from an API call.. This is the stringified version as well as below.

    sourceArray=

    [{"ID":"066CC1C6-924A-4300-86D4-2891B7FBA9AA","SunCo":"DEM","StartDate":"2012-05-25T14:45:07","EndDate":"2112-05-25T14:45:07","Message":"This is a test.\r\nThis is a test. This is test.\r\nThis is a test. This is a test.","Enabled":true,"LinkUrl":"","Rank":null},
    {"ID":"2AFBAD5B-FD3A-4502-BFD2-1B47100D7391","SunCo":"DEM","StartDate":"2015-12-13T13:17:49","EndDate":"2015-12-14T13:17:49","Message":"MESSAGE 11111","Enabled":true,"LinkUrl":"","Rank":null},
    {"ID":"2e716532-22d7-458b-a52c-b369c59b4e60","SunCo":"DEM","StartDate":"2015-12-13T00:00:00","EndDate":"2015-12-14T00:00:00","Message":"2222","Enabled":false,"LinkUrl":"","Rank":null},
    {"ID":"49A96E4A-AC27-4DA8-A6F0-744C4B57AB05","SunCo":"DEM","StartDate":"2015-12-13T13:17:54","EndDate":"2015-12-14T13:17:54","Message":"MESSAGE 11111","Enabled":true,"LinkUrl":"","Rank":null}]

    so i have sourceArray[0]

                   sourceArray[1]

    etc 

    How can i delete the following from the JSON object .. and return the new JSON object witht the deleted entries. where ID == 2AFBAD5B-FD3A-4502-BFD2-1B47100D7391 and 51A5C98A-F3AD-4E13-B6E1-114346F5EAE9

    removeArray=


    [{"ID":"2AFBAD5B-FD3A-4502-BFD2-1B47100D7391","SunCo":"DEM","StartDate":"2015-12-13T13:17:49","EndDate":"2015-12-14T13:17:49","Message":"MESSAGE 11111","Enabled":true,"LinkUrl":"","Rank":null}
    ,{"ID":"51A5C98A-F3AD-4E13-B6E1-114346F5EAE9","SunCo":"DEM","StartDate":"2015-12-13T13:17:53","EndDate":"2015-12-14T13:17:53","Message":"MESSAGE 11111","Enabled":true,"LinkUrl":"","Rank":null}]

    so i have removeArray[0]

                   removeArray[1]

    i would like to use the $.grep and $.each in jquery but am not too sure how,  I have used the following , 

    for (var k = 0; k < removeArray.length; k++)
    {
    var rem = removeAray[k];
    for (var i = 0; i < sourceArray.length; i++)
    {
    var cur = sourceArray[i];
    if (rem.ID == cur.ID)
    {
    sourceArray.splice(i, 1);
    break;
    }
    }
    }

    Thanks

    Sunday, December 13, 2015 3:25 AM

Answers

  • User61956409 posted

    Hi robby32,

    Please refer to the following sample code to filter javascript array and return a new array.

    <script>
        $(function () {
            var sourceArray = [{ "ID": "066CC1C6-924A-4300-86D4-2891B7FBA9AA", "SunCo": "DEM", "StartDate": "2012-05-25T14:45:07", "EndDate": "2112-05-25T14:45:07", "Message": "This is a test.\r\nThis is a test. This is test.\r\nThis is a test. This is a test.", "Enabled": true, "LinkUrl": "", "Rank": null },
     { "ID": "2AFBAD5B-FD3A-4502-BFD2-1B47100D7391", "SunCo": "DEM", "StartDate": "2015-12-13T13:17:49", "EndDate": "2015-12-14T13:17:49", "Message": "MESSAGE 11111", "Enabled": true, "LinkUrl": "", "Rank": null },
     { "ID": "2e716532-22d7-458b-a52c-b369c59b4e60", "SunCo": "DEM", "StartDate": "2015-12-13T00:00:00", "EndDate": "2015-12-14T00:00:00", "Message": "2222", "Enabled": false, "LinkUrl": "", "Rank": null },
     { "ID": "49A96E4A-AC27-4DA8-A6F0-744C4B57AB05", "SunCo": "DEM", "StartDate": "2015-12-13T13:17:54", "EndDate": "2015-12-14T13:17:54", "Message": "MESSAGE 11111", "Enabled": true, "LinkUrl": "", "Rank": null }];
    
            var IDs = ["2AFBAD5B-FD3A-4502-BFD2-1B47100D7391", "51A5C98A-F3AD-4E13-B6E1-114346F5EAE9"];
    
            var newsourceArray = [];
    
            for (var i = 0; i < sourceArray.length; i++) {
                var idval = sourceArray[i].ID.toString();
    
                if (idval != IDs[0] && idval != IDs[1]) {
    
                    newsourceArray.push(sourceArray[i]);
                }
            }
        })
    </script>
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 14, 2015 2:55 AM

All replies

  • User61956409 posted

    Hi robby32,

    Please refer to the following sample code to filter javascript array and return a new array.

    <script>
        $(function () {
            var sourceArray = [{ "ID": "066CC1C6-924A-4300-86D4-2891B7FBA9AA", "SunCo": "DEM", "StartDate": "2012-05-25T14:45:07", "EndDate": "2112-05-25T14:45:07", "Message": "This is a test.\r\nThis is a test. This is test.\r\nThis is a test. This is a test.", "Enabled": true, "LinkUrl": "", "Rank": null },
     { "ID": "2AFBAD5B-FD3A-4502-BFD2-1B47100D7391", "SunCo": "DEM", "StartDate": "2015-12-13T13:17:49", "EndDate": "2015-12-14T13:17:49", "Message": "MESSAGE 11111", "Enabled": true, "LinkUrl": "", "Rank": null },
     { "ID": "2e716532-22d7-458b-a52c-b369c59b4e60", "SunCo": "DEM", "StartDate": "2015-12-13T00:00:00", "EndDate": "2015-12-14T00:00:00", "Message": "2222", "Enabled": false, "LinkUrl": "", "Rank": null },
     { "ID": "49A96E4A-AC27-4DA8-A6F0-744C4B57AB05", "SunCo": "DEM", "StartDate": "2015-12-13T13:17:54", "EndDate": "2015-12-14T13:17:54", "Message": "MESSAGE 11111", "Enabled": true, "LinkUrl": "", "Rank": null }];
    
            var IDs = ["2AFBAD5B-FD3A-4502-BFD2-1B47100D7391", "51A5C98A-F3AD-4E13-B6E1-114346F5EAE9"];
    
            var newsourceArray = [];
    
            for (var i = 0; i < sourceArray.length; i++) {
                var idval = sourceArray[i].ID.toString();
    
                if (idval != IDs[0] && idval != IDs[1]) {
    
                    newsourceArray.push(sourceArray[i]);
                }
            }
        })
    </script>
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 14, 2015 2:55 AM
  • User1253338400 posted

    thanks for that  works great.

    Tuesday, December 15, 2015 10:13 AM