0

here i need the find the file extension among the given file names i.e i have 2 variable one is fileData which contains the file extensions and filename which contains the filename so here we have to check the whether the filenames having that particular extension or not if it is having then we have to retrieve the filename extension

fileData = ['exe', 'obj', 'file', 'data'];
filename = ['one.exe', 'two.obj', 'three.p', null, undefined];

constructor() {
  var fileSplit;
  for (var i of this.filename) {
    fileSplit = i.substring(i.lastIndexOf('.') + 1, i.length) || i;

    if (this.fileData.includes(fileSplit)) {

      console.log('File name::', i, 'and file extension::', fileSplit);
    } else {
      console.log('File name not there::', i)
    }
  }
}

GETTING THIS ERROR : ERROR

Error: Cannot read property 'substring' of null

stackblitz link https://stackblitz.com/edit/angular-ott3vh

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
sai686
  • 21
  • 7
  • Welcome! What is your expectation? sufficient or not means what? Can you write what you're expecting. – Kaushik Apr 16 '19 at 04:16
  • @Kaushik from fileData im providing a set of file extensions and using the filename im providing a group of file name now im trying check whether the filename having the particular extension or not if extension are having then we have to retreive the extension – sai686 Apr 16 '19 at 04:19
  • in the first line of for loop check whether the `i` is not equal to undefined or null. then proceed. – Kaushik Apr 16 '19 at 04:22
  • @Kaushik is my code my robust way to check like this ? or is there any best approach ? – sai686 Apr 16 '19 at 04:30
  • You can go with this! – Kaushik Apr 16 '19 at 04:32

3 Answers3

1

You are doing the split without checking for undefined cases. Do this:

fileData =['exe','obj','file','data'];
filename =['one.exe','two.obj','three.p',null,undefined];

constructor(){
   var fileSplit = "";
   for(var i of this.filename){
        fileSplit = "";
        if(i){ 
            fileSplit = i.substring(i.lastIndexOf('.')+1, i.length) || i;
            if(this.fileData.includes(fileSplit)){
                console.log('File name::',i,'and file extension::',fileSplit);
            } else{
                 console.log('File name not there::',i);
            }
        } // Do all the above if(i)
   }
}
Dblaze47
  • 868
  • 5
  • 17
  • is my code my robust way to check like this ? or is there any best approach ? – sai686 Apr 16 '19 at 04:30
  • Are you asking if your code is robust while performing the above task? – Dblaze47 Apr 16 '19 at 04:32
  • yes it fills my use case but still want to know is this the best approach – sai686 Apr 16 '19 at 04:34
  • Using regex is another option, but will be a bit expensive. This solution is pretty good I assume. Please check this answer for handling filename extensions: https://stackoverflow.com/a/1203361/6138595 – Dblaze47 Apr 16 '19 at 04:38
0

You can simply add short circuiting (&&) to check for true value and then use split

let fileData =['exe','obj','file','data'];
let filename =['one.exe','two.obj','three.p',null,undefined, 'some.some.data'];

function checkFile(fileData,fileName){
   var fileSplit;
   for(var i of filename){
     fileSplit = i && i.split('.')
     fileSplit = fileSplit && fileSplit[fileSplit.length-1]
  
     if(i && fileData.includes(fileSplit)){

       console.log('File name::',i,'and file extension::',fileSplit);
     }else{
       console.log('File name not there::',i)
     }
   }
}
   
checkFile(fileData, filename)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You have null and undefined in your fileName array. While iterating, you need to first check for null values and proceed only if it exists.

for(var i of this.filename){
   if (i == null || i==undefined){
      console.log('File name not there::',i)
   } else {
      fileSplit = i.substring(i.lastIndexOf('.')+1, i.length) || i;
      if(this.fileData.includes(fileSplit)){
         console.log('File name::',i,'and file extension::',fileSplit);
      } else {
         console.log('File extension not there::',i)
      }  
   }

}
Sachin Gupta
  • 4,981
  • 3
  • 16
  • 32