在这里我需要在给定的文件名中找到文件扩展名,即 我有2个变量,一个是 fileData ,它包含文件扩展名,另一个 filename ,它包含文件名,因此在这里,我们必须检查是否具有特定扩展名的文件名。然后必须检索文件扩展名
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)
}
}
}
获取此错误:错误
错误:无法读取null的属性“ substring”
stackblitz链接https://stackblitz.com/edit/angular-ott3vh
答案 0 :(得分:1)
您在进行拆分时未检查未定义的案例。这样做:
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)
}
}
答案 1 :(得分:0)
您只需添加short circuiting(ImportError: dlopen(/Users/paransonthalia/anaconda3/lib/python3.7/site-packages/fiona/ogrext.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libkea.1.4.7.dylib
)来检查真实值,然后使用split
&&
答案 2 :(得分:0)
您的fileName数组中有null
和undefined
。进行迭代时,您需要先检查null
值,然后仅在存在时进行操作。
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)
}
}
}