FYI: While loops also can be used to iterate forwards, but in truth there isn’t a great way. The problem with index based looping and TMaps is you can’t access a Key/Value pair by it’s index so it’s difficult to perform any type of conditional testing inside the loop. If at all possible, I’d manage a stale list of keys when doing “other” processing on the TMap and clean out the stale list at the end. But if you need to iterate over it. Assuming you have the following TMap:
TMap<FName, FString> TestMap;
You can do the following:
TArray<FName> MyKeys;
TestMap.GetKeys(MyKeys);
for (FName Key : Keys)
{
if (Key == FName(TEXT("DeleteKey"))
{
TestMap.Remove(Key);
}
}
TestMap.Compact();
That allows you to remove pairs from the TMap while still being able to use the TMap for testing/etc. But yea, sucks that you have to pull the keys to a secondary array. Oddly enough since removes from a TMap are soft until the TMap is Compacted, I think the iterator could be adjusted to allow for it in the case of a TMap. But since I almost never run in to this I’m not going to do it.