And you generally want for a key with 'key' in object, so for this code

    if (typeof item[key] === 'object' && 'duration' in item[key]) {