Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!