Last active
November 4, 2024 21:04
-
-
Save Ch-sriram/cdd2da35580876eb1595f15be445541c to your computer and use it in GitHub Desktop.
Get all possible key combinations in a deeply nested object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deeplyNestedObject = { | |
name: "John Doe", | |
age: 30, | |
address: { | |
street: "123 Main St", | |
city: "Somewhere", | |
postalCode: "12345", | |
coordinates: { | |
latitude: 37.7749, | |
longitude: -122.4194, | |
history: [ | |
{ year: 2020, location: "New York", events: ["moved", "started job"] }, | |
{ year: 2021, location: "Los Angeles", events: ["promoted", "bought a house"] } | |
] | |
} | |
}, | |
hobbies: [ | |
{ | |
name: "sports", | |
types: ["basketball", "soccer"], | |
favoriteTeams: { | |
basketball: "LA Lakers", | |
soccer: "FC Barcelona", | |
} | |
}, | |
{ | |
name: "music", | |
genres: ["rock", "jazz", "classical"], | |
favoriteArtists: [ | |
{ genre: "rock", artists: ["The Beatles", "Queen"] }, | |
{ genre: "jazz", artists: ["Miles Davis", "John Coltrane"] }, | |
{ genre: "classical", artists: ["Beethoven", "Mozart"] } | |
] | |
} | |
], | |
education: { | |
degrees: [ | |
{ | |
level: "Bachelor's", | |
major: "Computer Science", | |
university: "University of Somewhere", | |
graduationYear: 2015, | |
coursework: [ | |
{ name: "Algorithms", grade: "A" }, | |
{ name: "Operating Systems", grade: "B+" } | |
] | |
}, | |
{ | |
level: "Master's", | |
major: "Data Science", | |
university: "Tech University", | |
graduationYear: 2018, | |
coursework: [ | |
{ name: "Machine Learning", grade: "A+" }, | |
{ name: "Data Visualization", grade: "A" } | |
] | |
} | |
] | |
}, | |
workExperience: [ | |
{ | |
company: "TechCorp", | |
role: "Software Engineer", | |
years: 4, | |
projects: [ | |
{ | |
name: "Project Alpha", | |
description: "Built a web app for managing internal workflows.", | |
team: [{ name: "Alice", role: "Frontend" }, { name: "Bob", role: "Backend" }] | |
}, | |
{ | |
name: "Project Beta", | |
description: "Developed an API for real-time data analytics.", | |
team: [{ name: "Charlie", role: "Fullstack" }, { name: "Dana", role: "Data Scientist" }] | |
} | |
] | |
}, | |
{ | |
company: "DataInsights", | |
role: "Data Scientist", | |
years: 3, | |
projects: [ | |
{ | |
name: "Data Exploration Tool", | |
description: "Developed a tool for exploring large datasets.", | |
technologies: ["Python", "Pandas", "D3.js"], | |
results: { | |
efficiency: "Increased data processing by 30%", | |
userAdoption: "90% adoption rate among analysts" | |
} | |
} | |
] | |
} | |
] | |
}; | |
const allFields = []; | |
const getSearchableFields = (container, currentKey = null, keyTillNow = "") => { | |
if (Array.isArray(container) && container.length > 0) { | |
for (const element of container) { | |
getSearchableFields(element, currentKey, keyTillNow); | |
} | |
return; | |
} | |
if (typeof container === 'object') { | |
Object.keys(container).forEach(key => { | |
getSearchableFields(container[key], key, `${keyTillNow.length > 0 ? keyTillNow + '.' + key : key}`); | |
}, []); | |
} | |
allFields.push(keyTillNow); | |
}; | |
getSearchableFields(deeplyNestedObject); | |
console.log(allFields); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment