Bodrov
Bodrov

Reputation: 867

Performing function to see if JSON file includes specific strings

My local JSON file contains documents (ending in .docx, .pdf, etc). I have a table that's rendering those documents and I want to be able to render the text '.docx' (and what not) into a separate column, with each text string corresponding to a document. The end goal is to replace the texts with their respective icons, i.e. a MS Word one for .docx.

So far I have an .includes('docx' || 'DOCX' || 'pptx') method that correctly shows the values as true and false in the table's column:

|true||Example.docx|

|false||Other.popx|

How can I include a loop (or something else) in the code I have to display the texts of 'docx', 'pptx', etc. instead of true and false?

Loading doc title data:

loadTableData() {
    $.noConflict();
    let tableRes = KMdocs.d.results.filter(function(val) { 
      return (val.FileLeafRef.trim().length > 0);
    }).map(function(obj) {
      return {
        "Path": obj.EncodedAbsUrl,
        "Titles": obj.File.Name,
        "Categories": obj.ResourceType.results.map(function(val) {
          return val.Label;
        }).join(";"),
        "Blank": "",
        "docImg": obj.File.Name.includes('docx' || 'DOCX' || 'pptx')
        // ----- this is where I'd like to include the code
      }
    })

Rendering table data:

$('#km-table-id').DataTable( {
      columns: [
        // { data: "Blank" },
        { data: "Categories" }, // hidden
        { data: "docImg" },
        { data: "Titles" } // must be in last position to respond w/ checkboxes
      ],
      columnDefs: [
...etc

Upvotes: 0

Views: 43

Answers (1)

Kaizen Programmer
Kaizen Programmer

Reputation: 3818

This will filter for docx and pptx and then return the value if it's provided

function documentType(fileName) {
  return [fileName].filter(function(filter) {
    return filter.toLowerCase().includes('docx') || filter.toLowerCase().includes('pptx')
  }).map(function(filename) {
    //Get File Extension
    return filename.split('.').pop();
  }).pop();
}

Usage would be:

"docImg": documentType(obj.File.Name)

Upvotes: 1

Related Questions