Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda