本页尚未翻译,因此以英文显示。 参与翻译

omit

Returns a copy of a Map without the given keys.

Map<K, V> omit<K, V>(Iterable<K> keysToOmit, Map<K, V> map)

Lecture

omit builds a brand-new Map containing every entry of the original except the keys you list — the source map is never mutated. Keys in keysToOmit that don't actually exist in the map are simply ignored; there's no error for "extra" keys to remove.

It's a common redaction/serialization tool: strip a password hash before logging a user object, drop an internal field before sending a response, and so on. Pair it with map() to redact a whole list of maps at once.

Demo 1 · Basics

Demo 2 · Redacting a whole list

Try it yourself

Exercise: use omit to drop the 'debug' key from config.

Related: pick — the inverse: keep only some keys · omitBy — drop by predicate instead of key list · compactObject — drop null-valued keys · fromEntries — build a Map from scratch