I have an object:
"languages": {
"en":["au", "uk"],
"de":["de"],
....
}
How can I remove everything but a specified key, so if I specify 'en' I just want an object that contains "en":["au", "uk"]
General solution for the original question of 'how do I remove all keys except specified keys' (refined from Rajaprabhu's answer):
validKeys = [ 'a', 'b', 'c' ];
userInput = { "a":1, "b":2, "c":3, "d":4, "e":5 }
Object.keys(userInput).forEach((key) => validKeys.includes(key) || delete userInput[key]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With