How to exclude specific JSON objects when rendering data

Bodrov

I have a local JSON file containing JSON objects and I want it so that if an obj contains a specific string, then that object is rendered to a table. If not, then it's not rendered. For the time being I'm getting one error in DevTools: Uncaught TypeError: Cannot read property 'DT_RowId' of undefined and I'm working with DataTables---while it is useful, using functions with it has been a headache.

JS snippet:

function loadPH() {
    let admissText = admissData.d.results.map(function(val) {
        if (val.p_h_v !== "") { // If this is not empty, then return
            return {
                "PHV": val.p_h_v,
                "Part C": val.partc
            }
        }
    })

    $('#prohac-table').DataTable({
        columns: [
            { data: "PHV" },
            { data: "Part C" },
            ... // ---------- the rest contains irrelevant data

JSON snippet:

{
    "d": {
      "results": [
        {
         ...
         "p_h_v": "" // ------------ this doesn't meet conditions, isn't rendered
         ...
        },
        {
         "p_h_v": "Yes" // ---------- meets conditions---this obj rendered
         ...
Steve0

Converted your example to a stack-snippet and all it took was a filter, the values that map wasn't returning were undefined objects in your dataset and were causing the issue.

var admissData = {
  "d": {
    "results": [{
        "p_h_v": "Maybe", // ---------- meets conditions---this obj rendered
        "partc": "show this too"
      },
      {
        "p_h_v": "", // ------------ this doesn't meet conditions, isn't rendered
        "partc": "test - no show"
      },
      {
        "p_h_v": "Yes", // ---------- meets conditions---this obj rendered
        "partc": "test - show"
      }
    ]
  }
};

function loadProHac() {
  let admissText = admissData.d.results
    .filter(x => x.p_h_v !== "")  //added your filter here.
    .map(function(val) {
      return {
        "PHV": val.p_h_v,
        "Part C": val.partc
      }
    });

  $('#prohac-table').DataTable({
    data: admissText,
    columns: [{
        data: "PHV"
      },
      {
        data: "Part C"
      }
    ]
  });
}
loadProHac();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id='prohac-table'></table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to perform subquery when rendering json data with React?

How to prevent looping on specific objects found in json data

How to exclude specific type from json serialization

Extracting data from a JSON response in Python and exclude also specific data

How to handle missing JSON data when creating objects? (Typescript)

How do I exclude multi-line set of data when ONE line of data has a specific value? SQL-ORACLE

JSON marshaller ignored when rendering object with child objects

Undefined when parsing data and getting specific objects

How do I exclude null values when merging objects by key?

Firestore - how to exclude fields of data class objects in Kotlin

how do I exclude a specific folder on the destination when using rsync?

how to extract certain objects from Json file when specific object is matched

How to handle Objects with arrays to access specific data?

how to convert list of objects into specific data type

Exclude specific type in data frame

Rendering JSON for multiple Rails objects

Rails: rendering json with multiple objects

How to store JSON data from two sources in javascript objects when the data is pulled in at different rates?

Slow rendering of HTML table when binding JSON data

Strip JSON tags when rendering object data in angular

Mustache not rendering JSON data

Rendering json data with axios

Json data not rendering to the DOM

Rendering json data is not showing

How to take a list of Objects and create a JSON of specific properties of the objects in the list?

Rendering specific Content from json

How to exclude CustomResourceDefinition from rendering in Helm?

How to exclude parts of json, stored in Postgres, when selecting using ActiveRecord?

How to exclude a certain number of entries when calling upon a json feed

TOP Ranking

HotTag

Archive