Basically I have an array of files (which include file names and paths etc.) and I am trying to see if a file with the same pathname already exists in the array (even if the other variables are different).
If the value is found in that array I want to move on, if it is not I want to add that object to the array. Also giving me trouble is that the array is empty at first (so I can't even run my check function the first time without checking allFiles.length).
What I am doing is looping through the entire array, and setting a Boolean to true if I find the value contained in the array, then going on to use an if...else
I thought this would be simple but I guess I was mistaken. I came up with this method and I am wondering if there is a cleaner way to do it.
var allFiles = [];
//function is called giving me a file, then:
if (!allFiles.length) {
allFiles.push(file); //Seems like there should be a better way
} else {
for (var i = 0; i < allFiles.length; i += 1) {
var exists = false;
if (allFiles[i]['fileName'] == fileName) {
exists = true;
console.log('Already exists');
break;
}
}
if (exists) {
console.log('Remove the file');
exists = false;
} else {
console.log('Adding File');
allFiles.push(file);
}
}
Maybe that is clean enough, I was just wondering if you know of a different route. Thanks