вы храните все в памяти, принимая это как должное, вы можете начать с использования простого объекта, а не массива для хранения ваших данных
expArr = {}
, а затем добавить уникальный идентификатор, такой как хеш или дату в мс для каждого экземпляра
var exp = new Object();
exp.id = new Date().getUTCMilliseconds();
exp.name = req.body.name;
exp.value = req.body.val;
exp.date = req.body.date;
exp.description = req.body.descr;
expArr[exp.id] = exp;
теперь обязательно передайте от клиента правильный идентификатор, когда вы хотите удалить расходы
router.post('/removetodo', function (req, res, next) {
if(expArr[req.body.id]) {
delete expArr[req.body.id];
}
res.redirect("/");
});
InvalidPathChars устарел. Вместо этого используйте GetInvalidPathChars ():
public static bool FilePathHasInvalidChars(string path)
{
return (!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0);
}
Изменить: немного длиннее, но обрабатывает путь и недопустимые символы файла в одной функции:
// WARNING: Not tested
public static bool FilePathHasInvalidChars(string path)
{
bool ret = false;
if(!string.IsNullOrEmpty(path))
{
try
{
// Careful!
// Path.GetDirectoryName("C:\Directory\SubDirectory")
// returns "C:\Directory", which may not be what you want in
// this case. You may need to explicitly add a trailing \
// if path is a directory and not a file path. As written,
// this function just assumes path is a file path.
string fileName = System.IO.Path.GetFileName(path);
string fileDirectory = System.IO.Path.GetDirectoryName(path);
// we don't need to do anything else,
// if we got here without throwing an
// exception, then the path does not
// contain invalid characters
}
catch (ArgumentException)
{
// Path functions will throw this
// if path contains invalid chars
ret = true;
}
}
return ret;
}
Я рекомендую использовать HashSet
, чтобы это увеличило эффективность:
private static HashSet<char> _invalidCharacters = new HashSet<char>(Path.GetInvalidPathChars());
Затем можно просто проверить, что строка не является пустой/пустой и что нет никаких недопустимых символов:
public static bool IsPathValid(string filePath)
{
return !string.IsNullOrEmpty(filePath) && !filePath.Any(pc => _invalidCharacters.Contains(pc));
}