Ir para o conteúdo

CódigoNão tentada

Achatar um objeto aninhado

Dificuldade: média

{ user: { name: 'John', address: { city: 'NYC' } } } → { 'user.name': 'John', 'user.address.city': 'NYC' }.

  • itens de array usam seu índice: { tags: ['a'] } → { 'tags.0': 'a' }
  • null é um valor (cuidado com typeof null === 'object'); Dates também são valores
  • objetos aninhados vazios não geram chaves
Ver solução de referência
export function flattenObject(obj: Record<string, unknown>, parentKey = ''): Record<string, unknown> {
let result: Record<string, unknown> = {};
for (const key in obj) {
const newKey = parentKey ? `${parentKey}.${key}` : key;
const value = obj[key];
// typeof null is 'object' too, and a Date is a value, not something to open up.
if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
result = { ...result, ...flattenObject(value as Record<string, unknown>, newKey) };
} else {
result[newKey] = value;
}
}
return result;
}
Carregando editor…