Remove all elements from object except specified key?

panthro picture panthro · Apr 12, 2016 · Viewed 24.7k times · Source

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"]

Answer

isvforall picture isvforall · Apr 12, 2016

Simply, you could create a new object with specified field;

var key = 'en';

var o = {
    "languages": {
        "en": ["au", "uk"],
        "de": ["de"]
    }
}

var res = {}

res[key] = o.languages[key];