I want to create an array that contains the sum of a number in a given range below is the array that I am using
const data = [{
a: 0,
b: 0.75,
c: 0,
d: 0
},
{
a: 1,
b: 0.88,
c: 0,
d: 0
},
{
a: 2,
b: 0.38,
c: 0,
d: 1
},
{
a: 3,
b: 0.7,
c: 1,
d: 1
},
{
a: 4,
b: 0.93,
c: 1,
d: 0
}
];
In the above array, I want my output to look like something which is below
const output = [{
labelOfB = "0.3 - 0.4",
CountOfA = 1,
sumOfC = 1,
sumOfD = 1
},
{
labelOfB = "0.7 - 0.8",
CountOfA = 2,
sumOfC = 1,
sumOfD = 1
},
{
labelOfB = "0.8 - 0.9",
CountOfA = 1,
sumOfC = 0,
sumOfD = 0
},
{
labelOfB = "0.9 - 1",
CountOfA = 1,
sumOfC = 0,
sumOfD = 0
}
]
So here in the output what we are basically doing is we have a number provided by the user which will always be between 0 to 1 for 0.1, 0.05, or 0.2 so on, with this number we want to create a range
let's say the number is 0.1 so the range will be 0, 0.1, 0.2, up to 1
so with this range, I want my output to fall in this range only and it should never exceed than 1, so if we have a between 0.3 to 0.4 the output will be
{
labelOfB = "0.3 - 0.4", // range
CountOfA = 1, // no of A's in these range
sumOfC = 1, // sum of C in these range
sumOfD = 1 // sum of D in this range
}
I tried to do these from my end but I suck at for loop
const data = [{
a: 0,
b: 0.75,
c: 0,
d: 0,
},
{
a: 1,
b: 0.88,
c: 0,
d: 0,
},
{
a: 2,
b: 0.38,
c: 0,
d: 1,
},
{
a: 3,
b: 0.7,
c: 1,
d: 1,
},
{
a: 4,
b: 0.93,
c: 1,
d: 0,
},
{
a: 5,
b: 0.02,
c: 1,
d: 1,
},
{
a: 6,
b: 0.16,
c: 0,
d: 1,
},
{
a: 7,
b: 0.78,
c: 1,
d: 0,
},
];
const bin = 0.1; // this is the number by which will create range
let dataSet = {};
let CountOfa = 0;
let sumOfC = 0;
for (var i = 0; i < data.length; i ) {
for (var j = 0; j < 1; j = 0.1) {
if (j <= data[i].b && data[i].b <= bin j) {
CountOfa = 1;
sumOfC = sumOfC data[i].c;
dataSet = {
CountOfA: CountOfa,
labelOfB: `${j.toFixed(1)}-${(j bin).toFixed(1)}`,
sumOfC: sumOfC,
};
}
}
}
console.log(dataSet);
CodePudding user response:
you can do something like this
const bin = 0.1
const elaborate = (data, bin) =>
Array.from({length:Math.ceil(1 / bin)}, (_, i) => Number((bin * i).toFixed(3)))
.map(start => {
const inRange = data.filter(({b}) => b >= start && b < start bin)
return {
"CountOfA": inRange.length,
"labelOfB": `${start.toFixed(2)}-${(start bin).toFixed(2)}`,
"sumOfC": inRange.reduce((res, el) => res el.c, 0),
"sumOfD": inRange.reduce((res, el) => res el.d, 0),
}}).filter(d => d.CountOfA > 0)
const data = [{
a: 0,
b: 0.75,
c: 0,
d: 0
},
{
a: 1,
b: 0.88,
c: 0,
d: 0
},
{
a: 2,
b: 0.38,
c: 0,
d: 1
},
{
a: 3,
b: 0.7,
c: 1,
d: 1
},
{
a: 4,
b: 0.93,
c: 1,
d: 0
}
];
console.log(elaborate(data, bin))
console.log(elaborate(data, 0.05))
console.log(elaborate(data, 0.2))
CodePudding user response:
You could get the slot by using integer values and get the objects from a hash table.
const
data = [{ a: 0, b: 0.75, c: 0, d: 0 }, { a: 1, b: 0.88, c: 0, d: 0 }, { a: 2, b: 0.38, c: 0, d: 1 }, { a: 3, b: 0.7, c: 1, d: 1 }, { a: 4, b: 0.93, c: 1, d: 0 }, { a: 5, b: 0.02, c: 1, d: 1 }, { a: 6, b: 0.16, c: 0, d: 1 }, { a: 7, b: 0.78, c: 1, d: 0 }],
bin = 0.1, // this is the number by which will create range
result = Object.values(data.reduce((r, { b, c, d }) => {
const slot = Math.floor(b / bin);
r[slot] ??= { countOfA: 0, labelOfB: [slot * bin, (slot 1) * bin].map(v => v.toFixed(1)).join('-'), sumOfC: 0, sumOfD: 0 };
r[slot].countOfA ;
r[slot].sumOfC = c;
r[slot].sumOfD = d;
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }