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

132
Vistas
create and count dynamic object in typescript

I am trying to compute a result from an array for objects that I can count how many values for each key and add its own dbId to the same computed result within the object name! the reason behind that is to use it to chart.js. so I can check each result value comes with its own dbIds

const obj = [
  {
    dbId: 26598,
    properties: [
      { attributeName: "BIM7AATypeCode" },
      { displayCategory: "Identity Data" },
      { displayName: "BIM7AATypeCode" },
      { displayValue: "221" },
    ],
  },
  {
    dbId: 26591,
    properties: [
      { attributeName: "BIM7AATypeCode" },
      { displayCategory: "Identity Data" },
      { displayName: "BIM7AATypeCode" },
      { displayValue: "221" },
    ],
  },
  {
    dbId: 3695,
    properties: [
      { attributeName: "abc" },
      { displayCategory: "Identity Data" },
      { displayName: "BIM7AATypeCode" },
      { displayValue: "123" },
    ],
  },
  {
    dbId: 3697,
    properties: [
      { attributeName: "abc" },
      { displayCategory: "Identity Data" },
      { displayName: "BIM7AATypeCode" },
      { displayValue: "123" },
    ],
  },
];

// type Histogram = {
//   [key: string]: number;
// };

let histogram = {}; //as Histogram

for (let iterator of obj) {
  for (let { displayName, displayValue } of iterator.properties) {
    //@ts-ignore
    histogram[displayName] = histogram[displayName] | {};

    if (!histogram[displayValue]) {
      histogram[displayValue] = 1;
    } else {
      histogram[displayValue] = histogram[displayValue] + 1;
    }
  }
}

console.log(histogram);

//expected result:

/*

{
  BIM7AATypeCode: {
    "221": 2,
    dbid: [26598, 26591],
  },
  abc: {
    "123": 2,
    dbid: [3695, 3697],
  },
};
*/

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

0

Please find the Array.reduce implementation.

Logic

  • Loop through obj array using Array.reduce
  • From each object in the array, pick the properties key. From this select node having key attributeName which is attribute node and with displayValue which is display value node.
  • Check whether the accumulator has a node with the attributeName.
  • If not, push a node to accumulator, with the displayValue having vale 1 and dbid as a single noded array.
  • If accumulator already have this node. Update the count of displayValue and push the new dbid

const obj = [
  { dbId: 26598, properties: [{ attributeName: "BIM7AATypeCode" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "221" }] },
  { dbId: 26591, properties: [{ attributeName: "BIM7AATypeCode" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "221" }] },
  { dbId: 3695, properties: [{ attributeName: "abc" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "123" }] },
  { dbId: 3697, properties: [{ attributeName: "abc" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "123" }] },
];
const output = obj.reduce((acc, curr) => {
  const attribute = curr.properties.find(node => node.attributeName);
  const displayValue = curr.properties.find(node => node.displayValue);
  if (acc[attribute.attributeName]) {
    acc[attribute.attributeName][displayValue.displayValue] ? acc[attribute.attributeName][displayValue.displayValue]++ : acc[attribute.attributeName][displayValue.displayValue] = 1;
    acc[attribute.attributeName].dbid.push(curr.dbId);
  } else {
    const newAttribute = {
      [displayValue.displayValue]: 1,
      dbid: [curr.dbId]
    }
    acc[attribute.attributeName] = newAttribute;
  }
  return acc;
}, {});
console.log(output);

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