Saturday, November 28, 2015

Remove JSON properties

We can easily remove specific properties from a JSON object in an array.

Edit in plunkr

Steps involved

  • Loop through the JSON object
  • Specify the properties which needs to be filtered

In the example below I was supposed to remove some properties. I have removed "Date" property.




<div class="mydiv">
    <textarea cols="100" rows="15" class="txtarea" id="txt">[{"Blog Name":"PrathapKudupusBlog","Date":"30 Jul 2013     09:24 AM","Type":"Technical","Author":"Prathap Kudupu"},{"Blog Name":"ABCBlog","Date":"30 Jul 2011 09:24 AM",     "Type":"Technical","Author":"ABC"},{"Blog Name":"XYZBlog","Date":"30 Jul 2011 09:24 AM","Type":"Technical","Author":"XYZ"}]</textarea>     
       <button class="download">Download CSV</button>
    </div>
<script>
$(document).ready(function(){
    $('button').click(function(){
        var data = $('#txt').val();
         for (var i = 0; i < data.length; i++) {
                delete data[i]["Date"];
         }
        JSONToCSVConvertor(data, "Blog report", true);
    });
});
</script>
Output 

Before 



 After



No comments:

Post a Comment