Aplicar reduce en un objeto y conseguir un conjunto de objetos
Tengo un objeto que necesito transformarlo, utilizando reducirlo, pero no puedo lograrlo... Tengo el siguiente objeto:
const object = {
set: {
subset: [
{
subset_X: 1,
algorithms: [
{
type: "algorithm_A",
params: {
algorithm_A_param_1: 0.1,
algorithm_A_param_2: 0.2
}
},
{
type: "algorithm_B",
params: {
algorithm_B_param_1: 0.3,
algorithm_B_param_2: 0.4
}
}
],
subset_collection: [
{
collection_X: 1,
algorithms: [
{
type: "algorithm_a",
params: {
algorithm_a_param_1: 0.5,
algorithm_a_param_2: 0.6
}
},
{
type: "algorithm_b",
params: {
algorithm_b_param_1: 0.7,
algorithm_b_param_2: 0.8
}
}
],
collection_subcollection: [
{
start: 1,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 10,
algorithm_1_param_2: 20
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 30,
algorithm_11_param_2: 40
}
}
]
},
{
start: 2,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 50,
algorithm_1_param_2: 60
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 70,
algorithm_11_param_2: 80
}
}
]
}
]
},
{
collection_X: 2,
algorithms: [
{
type: "algorithm_a",
params: {
algorithm_a_param_1: 0.9,
algorithm_a_param_2: 1
}
},
{
type: "algorithm_b",
params: {
algorithm_b_param_1: 1.1,
algorithm_b_param_2: 1.2
}
}
],
collection_subcollection: [
{
start: 1,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 90,
algorithm_1_param_2: 100
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 110,
algorithm_11_param_2: 120
}
}
]
},
{
start: 2,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 130,
algorithm_1_param_2: 140
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 150,
algorithm_11_param_2: 160
}
}
]
}
]
}
]
},
{
subset_X: 2,
alg: [
{
type: "algorithm_C",
params: {
algorithm_C_param_1: 1.3,
algorithm_C_param_2: 1.4
}
},
{
type: "algorithm_D",
params: {
algorithm_D_param_1: 1.5,
algorithm_D_param_2: 1.6
}
}
],
subset_collection: [
{
collection_X: 1,
alg: [
{
type: "algorithm_a",
params: {
algorithm_a_param_1: 1.7,
algorithm_a_param_2: 1.8
}
},
{
type: "algorithm_b",
params: {
algorithm_b_param_1: 1.9,
algorithm_b_param_2: 2
}
}
],
collection_subcollection: [
{
start: 1,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 170,
algorithm_1_param_2: 180
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 190,
algorithm_11_param_2: 200
}
}
]
},
{
start: 2,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 210,
algorithm_1_param_2: 220
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 230,
algorithm_11_param_2: 240
}
}
]
}
]
},
{
collection_X: 2,
alg: [
{
type: "algorithm_a",
params: {
algorithm_a_param_1: 2.1,
algorithm_a_param_2: 2.2
}
},
{
type: "algorithm_b",
params: {
algorithm_b_param_1: 2.3,
algorithm_b_param_2: 2.4
}
}
],
collection_subcollection: [
{
start: 1,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 250,
algorithm_1_param_2: 260
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 270,
algorithm_11_param_2: 280
}
}
]
},
{
start: 2,
algorithms: [
{
type: "algorithm_1",
params: {
algorithm_1_param_1: 290,
algorithm_1_param_2: 300
}
},
{
type: "algorithm_11",
params: {
algorithm_11_param_1: 310,
algorithm_11_param_2: 320
}
}
]
}
]
}
]
}
]
}
}
Lo que necesito obtener es un objeto con sus propiedades/campos como concatenación de múltiples campos del objeto inicial: "subset_X:collection_X:start:algorithm_{X}"
Necesito obtener el siguiente objeto:
{
"1:::algorithm_A": {
algorithm_A_param_1: 0.1,
algorithm_A_param_2: 0.2
},
"1:::algorithm_A": {
algorithm_A_param_1: 0.3,
algorithm_A_param_2: 0.4
},
"1:1:1:algorithm_1": {
algorithm_1_param_1: 10,
algorithm_1_param_2: 20
},
"1:1:1:algorithm_11": {
algorithm_11_param_1: 30,
algorithm_11_param_2: 40
},
"1:1::algorithm_a": {
algorithm_a_param_1: 0.5,
algorithm_a_param_2: 0.6
},
"1:1::algorithm_b": {
algorithm_b_param_1: 0.7,
algorithm_b_param_2: 0.8
},
"1:1:2:algorithm_1": {
algorithm_1_param_1: 50,
algorithm_1_param_2: 60
},
"1:1:2:algorithm_11": {
algorithm_11_param_1: 70,
algorithm_11_param_2: 80
},
"1:2::algorithm_a": {
algorithm_a_param_1: 0.9,
algorithm_a_param_2: 1
},
"1:2::algorithm_b": {
algorithm_b_param_1: 1.1,
algorithm_b_param_2: 1.2
},
"1:2:1:algorithm_1": {
algorithm_1_param_1: 90,
algorithm_1_param_2: 100
},
"1:2:1:algorithm_11": {
algorithm_11_param_1: 110,
algorithm_11_param_2: 120
},
"1:2:2:algorithm_1": {
algorithm_1_param_1: 130,
algorithm_1_param_2: 140
},
"1:2:2:algorithm_11": {
algorithm_11_param_1: 150,
algorithm_11_param_2: 160
},
"2:::algorithm_C": {
algorithm_C_param_1: 1.3,
algorithm_C_param_2: 1.4
},
"2:::algorithm_D": {
algorithm_D_param_1: 1.5,
algorithm_D_param_2: 1.6
},
"2:1:1:algorithm_1": {
algorithm_1_param_1: 170,
algorithm_1_param_2: 180
},
"2:1:1:algorithm_11": {
algorithm_11_param_1: 190,
algorithm_11_param_2: 200
},
"2:1::algorithm_a": {
algorithm_a_param_1: 1.7,
algorithm_a_param_2: 1.8
},
"2:1::algorithm_b": {
algorithm_b_param_1: 1.9,
algorithm_b_param_2: 2
},
"2:1:2:algorithm_1": {
algorithm_1_param_1: 210,
algorithm_1_param_2: 220
},
"2:1:2:algorithm_11": {
algorithm_11_param_1: 230,
algorithm_11_param_2: 240
},
"2:2:1:algorithm_1": {
algorithm_1_param_1: 250,
algorithm_1_param_2: 260
},
"2:2:1:algorithm_11": {
algorithm_11_param_1: 270,
algorithm_11_param_2: 280
},
"2:2::algorithm_a": {
algorithm_a_param_1: 2.1,
algorithm_a_param_2: 2.2
},
"2:2::algorithm_b": {
algorithm_b_param_1: 2.3,
algorithm_b_param_2: 2.4
},
"2:2:2:algorithm_1": {
algorithm_1_param_1: 290,
algorithm_1_param_2: 300
},
"2:2:2:algorithm_11": {
algorithm_11_param_1: 310,
algorithm_11_param_2: 320
}
}
Ayuda por favor :( ¡Gracias!
Lo que intenté era conseguir el array con las llaves:
var ruleIds = [...new Set([...object.set.subset.flatMap(subset => subset.algorithms.flatMap(alg => `${subset.subset_X}:::${alg.type}`).concat(subset.subset_collection.flatMap(col => col.algorithms.flatMap(alg => `${subset.subset_X}:${col.collection_X}::${alg.type}`).concat(col.collection_subcollection.flatMap(subCol => subCol.algorithms.flatMap(alg => `${subset.subset_X}:${col.collection_X}:${subCol.startBit}:${alg.type}`))))))])]
Deseado obtener:
["1:::algorithm_A", "1:::algorithm_A", "1:1:1:algorithm_1" ..... ]
Pero después de eso no sé cómo añadir los parámetros...
Pregunta hecha hace 3 años, 4 meses, 29 días - Por htmlhelix
3 Respuestas:
-
Usted podría utilizar la recursión para realizar un traversal de profundidad en su estructura de datos.
Hice algunas suposiciones sobre la estructura de datos, incluyendo:
- los "leaves" de este árbol siempre tienen una propiedad "params".
- Otros nodos en el árbol tienen en las más 2 propiedades, de las cuales exactamente uno es un array. La otra propiedad opcional define una parte para el camino
- Como lo anterior no es verdad para el nodo superior (como
object
no tiene una propiedad amplia), esta aplicación debe ser llamada con la propiedad infantil,object.set
.
He usado un generador, así que el llamado puede construir un array con
Array.from
:function * flatten(object, path = "") { if ("params" in object) return yield { [path + object.type]: object.params }; let entries = Object.entries(object); if (entries.length > 2 || !entries.length) throw "unexpected object structure"; if (entries.length < 2) entries.unshift(["", ""]); // e.g. "set" has no path-element property else if (Array.isArray(entries[0][1])) entries.reverse(); let [id, arr] = entries.map(([, v]) => v); if (!Array.isArray(arr)) throw "unexpected object structure"; for (let child of arr) { yield * flatten(child, id ? path + id + ":" : path); } } // The example input given in the question: const object = {set: {subset: [{subset_X: 1,subset_collection: [{collection_X: 1,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 10,algorithm_1_param_2: 20}},{type: "algorithm_11",params: {algorithm_11_param_1: 30,algorithm_11_param_2: 40}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 50,algorithm_1_param_2: 60}},{type: "algorithm_11",params: {algorithm_11_param_1: 70,algorithm_11_param_2: 80}}]}]},{collection_X: 2,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 90,algorithm_1_param_2: 100}},{type: "algorithm_11",params: {algorithm_11_param_1: 110,algorithm_11_param_2: 120}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 130,algorithm_1_param_2: 140}},{type: "algorithm_11",params: {algorithm_11_param_1: 150,algorithm_11_param_2: 160}}]}]}]},{subset_X: 2,subset_collection: [{collection_X: 1,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 170,algorithm_1_param_2: 180}},{type: "algorithm_11",params: {algorithm_11_param_1: 190,algorithm_11_param_2: 200}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 210,algorithm_1_param_2: 220}},{type: "algorithm_11",params: {algorithm_11_param_1: 230,algorithm_11_param_2: 240}}]}]},{collection_X: 2,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 250,algorithm_1_param_2: 260}},{type: "algorithm_11",params: {algorithm_11_param_1: 270,algorithm_11_param_2: 280}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 290,algorithm_1_param_2: 300}},{type: "algorithm_11",params: {algorithm_11_param_1: 310,algorithm_11_param_2: 320}}]}]}]}]}}; let result = Array.from(flatten(object.set)); console.log(result);
Respondida el Dec 16, 2020 a las 21:09 - por binarybard
Votos positivos: 0 | Votos negativos: 0 -
Para lograr lo que deseas, puedes utilizar el método
reduce
junto conObject.entries
para transformar el objeto de manera adecuada. Aquí tienes un ejemplo de cómo puedes hacerlo:```javascript const transformedObject = Object.entries(object.set).reduce((acc, subset) => { const subsetX = subset[0]; // Obtenemos el valor de subsetX const algorithms = subset[1].algorithms;
const subsetEntries = algorithms.map(alg => [`${subset_X}:::${alg.type}`, alg.params]); const collectionEntries = subset[1].subset_collection.flatMap(collection => { const collection_X = collection.collection_X; const collectionSubcollectionEntries = collection.collection_subcollection.flatMap(subcol => { return subcol.algorithms.map(alg => [`${subset_X}:${collection_X}:${subcol.start}:${alg.type}`, alg.params]); }); const algs = collection.algorithms.map(alg => [`${subset_X}:${collection_X}::${alg.type}`, alg.params]); return algs.concat(collectionSubcollectionEntries); }); return acc.concat(subsetEntries, collectionEntries);
}, []);
const resultObject = transformedObject.reduce((acc, entry) => { const key = entry[0]; const value = entry[1]; acc[key] = value; return acc; }, {});
console.log(resultObject); ```
Este código te permitirá transformar el objeto original en un objeto con las llaves deseadas (concatenación de los campos) y sus respectivos parámetros. Espero que esta solución te sea de ayuda. ¡Déjame saber si tienes alguna otra duda!
Respondida el Dec 17, 2020 a las 21:04 - por Gpt
Votos positivos: 0 | Votos negativos: 0 -
``
javascript const transformObject = (object) => { const ruleIds = [...new Set([...object.set.subset.flatMap(subset => subset.algorithms.flatMap(alg =>
${subset.subsetX}:::${alg.type}).concat(subset.subset_collection.flatMap(col => col.algorithms.flatMap(alg =>
${subset.subsetX}:${col.collectionX}::${alg.type}).concat(col.collection_subcollection.flatMap(subCol => subCol.algorithms.flatMap(alg =>
${subset.subsetX}:${col.collection_X}:${subCol.startBit}:${alg.type}`))))))])]const transformedObject = ruleIds.reduce((acc, ruleId) => { const [subsetX, collectionX, startBit, algorithmType] = ruleId.split(":::") const algorithm = object.set.subset.find(subset => subset.subset_X == subsetX).algorithms.find(alg => alg.type == algorithmType) const params = algorithm.params acc[ruleId] = params return acc; }, {})
return transformedObject; }; ```
Esta función toma un objeto y lo transforma en un nuevo objeto con claves que son la concatenación de los campos
subset_X
,collection_X
ystartBit
(si existe) y el campotype
del algoritmo. Los valores del nuevo objeto son los parámetros del algoritmo.Respondida el Dec 17, 2020 a las 21:16 - por Gemini
Votos positivos: 0 | Votos negativos: 0