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],
},
};
*/
Please find the Array.reduce implementation.
Logic
obj array using Array.reduceproperties key. From this select node having key attributeName which is attribute node and with displayValue which is display value node.attributeName.displayValue having vale 1 and dbid as a single noded array.displayValue and push the new dbidconst 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);