Q:

Recursive removal of arrays inside javascript object

I have the following object:
{ name: ["Jimmy","Jill"], age: [23, 42], location: { city: ["LA", "NYC"] }

For every object key there is an array value with 2 entries (always). What I'm trying to do is to recreate the object by plucking the 2nd item in the arrays. But I want this to happen recursively. So the output would be:
{ name: "Jill", age: 42, location: { city: "NYC" }

I have tried iterating through the object using Object.keys() but this doesn't appear to give me nested keys. I was wondering if there was a more elegant way to tackle this?
Many thanks in advance. 

A:

Is this considered okay? This only works if we assume every prop would either be an Array of a plain Object.

let o = { name: ["Jimmy","Jill"], age: [23, 42], location: { city: ["LA", "NY"] }}

function rec(obj) {
    for (let key in obj) {
        if (obj[key] instanceof Array) {
            obj[key] = obj[key][1]
        } else {
            obj[key] = rec(obj[key])
        }
    }
    return obj
}

rec(o)
console.dir(o)

Or even this? (allows existence of array that contains objects and plain primitive entries)

let o = {
    primitive: 10,
    name: ["Jimmy","Jill"],
    age: [23, 42],
    location: {
        city: ["LA", "NY"],
        test: [
            {prop1: ['1', '2']},
            {prop2: ['A', 'B']}
        ]
    }
}

function rec(obj) {
    if (obj instanceof Array) {
        return rec(obj[1])
    } else if (obj instanceof Object) {
        for (let key in obj) {
            obj[key] = rec(obj[key])
        }
        return obj
    } else {
        return obj
    }
}

rec(o)
console.dir(o)

