Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
Separating (n) keys from array of objects into a single array with keys names

I need to perform filter in the array of objects to get all the keys. Although, whenever there is a obj inside of that key, I would need to get the key name and concat with the key name from the obj, so for example:

const data = [ id: 5, name: "Something", obj: { lower: True, higher: False } ]
result = ["id", "name", "obj.lower", "obj.higher"]

I could manage to do the above code, but, if there is more objs inside the data, I would need to keep adding a if condition inside of my logic, I would like to know if there is any other way, so it doesn't matter how many objects I have inside the objects, It will concat always.
The code I used from the above mention:

const itemsArray = [
      { id: 1, item: "Item 001", obj: { name: 'Nilton001', message: "Free001", obj2: { test: "test001" } } },
      { id: 2, item: "Item 002", obj: { name: 'Nilton002', message: "Free002", obj2: { test: "test002" } } },
      { id: 3, item: "Item 003", obj: { name: 'Nilton003', message: "Free003", obj2: { test: "test003" } } },
    ];

const csvData = [    
    Object.keys(itemsArray[0]),
    ...itemsArray.map(item => Object.values(item))
].map(e => e.join(",")).join("\n")

// Separating keys
let keys = []
const allKeys = Object.entries(itemsArray[0]);
for (const data of allKeys) {
    if (typeof data[1] === "object") {
        const gettingObjKeys = Object.keys(data[1]);
        const concatingKeys = gettingObjKeys.map((key) => data[0] + "." + key);        
        keys.push(concatingKeys);
    } else {
        keys.push(data[0])
    }
}

//Flating
const flattingKeys = keys.reduce((acc, val: any) => acc.concat(val), []);

What I would like to achieve, lets suppose I have this array of object:

const data = 
[
   { id: 10, obj: {name: "Name1", obj2: {name2: "Name2", test: "Test"}}}
   ...
]

Final result = ["id", "obj.name", "obj.obj2.name2", "obj.obj2.test"]

OBS: The first obj contains all the keys I need, no need to loop through other to get KEYS.

I would like to achieve, all the keys from the first object of the array, and if there is objects inside of objects, I would like to concat the obj names (obj.obj2key1)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could map the key or the keys of the nested objects.

const
    getKeys = object => Object
        .entries(object)
        .flatMap(([k, v]) => v && typeof v === 'object'
            ? getKeys(v).map(s => `${k}.${s}`)
            : k
        ),
    getValues = object => Object
        .entries(object)
        .flatMap(([k, v]) => v && typeof v === 'object'
            ? getValues(v)
            : v
        ),
    data = { id: 1, item: "Item 001", obj: { name: 'Nilton001', message: "Free001", obj2: { test: "test001" } } },
    keys = getKeys(data),
    values = getValues(data);

console.log(keys);
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

something like this

const itemsArray = [
      { id: 1, item: "Item 001", obj: { name: 'Nilton001', message: "Free001", obj2: { test: "test001" } } },
      { id: 2, item: "Item 002", obj: { name: 'Nilton002', message: "Free002", obj2: { test: "test002" } } },
      { id: 3, item: "Item 003", obj: { name: 'Nilton003', message: "Free003", obj2: { test: "test003" } } },
    ];
    
const item = itemsArray[0];

const getAllKeys = (obj, prefix=[]) => {
 if(typeof obj !== 'object'){
   return prefix.join('.')
 }
 return Object.entries(obj).flatMap(([k, v]) => getAllKeys(v, [...prefix, k]))
}

console.log(getAllKeys(item))

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use recursion as follows. Since typeof([1,3,5]) is object, we also have to confirm that value is not an array, !Array.isArray(value):

const obj = { id: 10, obj: {name: "Name1", obj2: {name2: "Name2", test: "Test"}}};

const getKeys = (o,p) => Object.entries(o).flatMap(([key,value]) => 
    typeof(value) === 'object' && !Array.isArray(value) ? 
        getKeys(value, (p?`${p}.`:"") + key) : 
        (p ? `${p}.`: "") + key
);

console.log( getKeys(obj) );

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda