Render last 12 months for data template - javascript

I'm struggling with creating correct data template for my chart. I want it to create template for last 12 months with actual month. For example if I want to have last 12 months i should have data template looking like this:
[{id: BAD,
data: [
{
x: "11",
y: 0
},
{
x: "12",
y: 0
},
{
x: "1",
y: 0
},
...
]},
{id: GOOD,
data: [
{
x: "11",
y: 0
},
{
x: "12",
y: 0
},
{
x: "1",
y: 0
},
...
]},
...
]
It is not that simple because I don't know what to do when month increases to 12 because then its just keeps increasing value of "x" which stands for month and thus I don't know how should I implement that.
What i tried to do was just this. I have no other clues how to get this any hints how I can get that?
const NUMBER_OF_MONTHS = 12;
const getFirstMonth = (date, numOfMonths) => {
date.setMonth(date.getMonth() - numOfMonths);
return date.getMonth();
}
const createDataTemplate = () => {
const template = [];
const firstMonth = getFirstMonth(new Date(), NUMBER_OF_MONTHS)
for (let i = 0; i < RATINGS.length; i++) {
template.push({
'id': RATINGS[i],
'data': []
})
for (let j = 1; j <= NUMBER_OF_MONTHS; j++) {
template[i].data.push({
'x': `${firstMonth + j}`,
'y': 0
})
}
}
return template;
}

I solved it like this, generating an array of the next 12 months/year. Which you can then use to loop over and add to you data
const NUMBER_OF_MONTHS = 12;
const getNextTwelveMonths = () => {
const currentMonth = new Date().getMonth();
// create array with the months until the currentMonth
const months = new Array(currentMonth).fill(null).map((_, i) => i + 1);
// add the last x months to the begin of the array
for (let i = NUMBER_OF_MONTHS; i > currentMonth; i--) {
months.unshift(i);
}
return months;
};
const createDataTemplate = () => {
const template = [];
for (let i = 0; i < RATINGS.length; i++) {
template.push({
id: RATINGS[i],
data: [],
});
const nextMonths = getNextTwelveMonths();
for (let j = 0; j < nextMonths.length; j++) {
template[i].data.push({
x: `${nextMonths[j]}`,
y: 0,
});
}
}
return template;
};

Related

Javascript make calculations and filter list

I have below array -
const data=[
{
month:"nov",
veryLate:3,
Late:5,
onTime:2
},
{
month:"dec",
veryLate:1,
Late:3,
onTime:16
},
{
month:"jan",
veryLate:28,
Late:1,
onTime:1
},
}
I want to filter and make calculations on this array such that percentage can be obtained.
Eg. veryLate + Late+ onTime = (3+5+2) = 10
So percentage wise it is -
const data= [
{
month:"nov",
veryLate:30,
Late:50,
onTime:20
},
{
month:"dec",
veryLate:5,
Late:15,
onTime:80
},
,
{
month:"jan",
veryLate:98.33,
Late:3.33,
onTime:3.33
},]
To calculate this I had performed below , but getting syntax error over brackets -
var filteredData=data.map(x=>{
x.month,
x.veryLate/(x.veryLate+x.Late+x.onTime)*100,
x.Late/(x.veryLate+x.Late+x.onTime)*100,
x.onTime/(x.veryLate+x.Late+x.onTime)*100,
});
How can I obtained calculated results?
x.veryLate wont work in x it should be veryLate itself same for the others
const data=[
{
month:"nov",
veryLate:3,
Late:5,
onTime:2
},
{
month:"dec",
veryLate:1,
Late:3,
onTime:16
},
{
month:"jan",
veryLate:28,
Late:1,
onTime:1
},
]
var filteredData= data.map(x => (
{
...x,
veryLate: x.veryLate/(x.veryLate+x.Late+x.onTime)*100,
Late: x.Late/(x.veryLate+x.Late+x.onTime)*100,
onTime: x.onTime/(x.veryLate+x.Late+x.onTime)*100
})
)
console.log(filteredData)
You also must wrap the returning object literal into parentheses. Currently the curly braces is being denoted as the function body.
The map() method needs to return a value.
Try this
var filteredData=data.map(x => {
// get the original values to avoid repeating x.
const {month, veryLate, Late, onTime} = x;
// do your calculations
const newVeryLate = veryLate / ( veryLate + Late + onTime) * 100;
const newLate = Late / (veryLate + Late + onTime) * 100;
const newOnTime = onTime / (veryLate + Late + onTime) * 100;
// return the new object
return {month, veryLate: newVeryLate, Late: newLate, onTime: newOnTime}
});
var filteredData=data.reduce((a, v)=>{
let obj = {};
let sum = v.veryLate+v.Late+v.onTime;
obj.month = v.month;
obj.veryLate = v.veryLate/sum*100;
obj.Late = v.Late/sum*100;
obj.onTime = v.onTime/sum*100;
a.push(obj)
return a;
}, []);
Throwing error because:
The data array has no closing bracket
map method is not returning anything. if you want to return an object from it use return keyword.
The problem is that the callback passed to Array.prototype.map needs to return something for every iteration and in your current implementation you aren't returning anything.
You can use map as shown below:
const data = [
{ month: "nov", veryLate: 3, Late: 5, onTime: 2 },
{ month: "dec", veryLate: 1, Late: 3, onTime: 16 },
{ month: "jan", veryLate: 28, Late: 1, onTime: 1 },
];
const filteredData = data.map(({ month, veryLate, Late, onTime }) => {
const total = veryLate + Late + onTime;
return {
month,
veryLate: (veryLate / total) * 100,
Late: (Late / total) * 100,
onTime: (onTime / total) * 100,
};
});
console.log(filteredData);
If you're finding map confusing, you can also do it with a regular for loop, as shown below:
const data = [
{ month: "nov", veryLate: 3, Late: 5, onTime: 2 },
{ month: "dec", veryLate: 1, Late: 3, onTime: 16 },
{ month: "jan", veryLate: 28, Late: 1, onTime: 1 },
];
const filteredData = [];
for (let i = 0; i < data.length; i++) {
const { month, veryLate, Late, onTime } = data[i];
const total = veryLate + Late + onTime;
result.push({
month,
veryLate: (veryLate / total) * 100,
Late: (Late / total) * 100,
onTime: (onTime / total) * 100,
});
}
console.log(filteredData);
Additional Documentation:
Object Destructuring

Feeding Array to a Classifier

I have an issue looping through an array using a bayesian classifier function.
Here is my array:
var data = ['good', {
dry: 1,
wet: 0,
moist:0
}, 'bad', {
dry: 0,
wet: 1,
moist: 1
}, 'neutral', {
dry: 1,
wet: 1,
moist:1
}, 'good', {
dry: 1,
wet: 0,
moist: 1
}];
Here's my classifier function:
class Bayes{
constructor(...categories) {
this.categories = {};
this.categoryCounts = {};
categories.forEach(category => {
this.categories[category] = {};
this.categoryCounts[category] = 0;
});
}
train(category, dataset) {
this.categoryCounts[category]++;
Object.keys(dataset).forEach(key => {
this.categories[category][key] = (this.categories[category][key] || '') + dataset[key];
});
};
classify(dataset) {
let scores = {};
let trainingCount = Object.values(this.categoryCounts).reduce((a, b) => a + b );
Object.keys(this.categories).forEach(category => {
scores[category] = 0;
let categoryWords = this.categories[category];
let total = Object.values(categoryWords).reduce((a, b) => a + b );
Object.keys(dataset).forEach(function (key) {
let value = dataset[key];
let s = categoryWords[key] || 0.1;
let i = 0;
while(i<value){
scores[category] += Math.log(s / parseFloat(total));
i++;
}
});
let s = this.categoryCounts[category] || 0.1;
scores[category] = (s / trainingCount);
});
return scores;
};
};
Normally, to classify the data; I'll do:
var b = new Bayes('good', 'bad', 'neutral');
b.train('good', { dry: 1, wet: 0, moist:0});
b.train('bad', {dry: 0,wet: 1,moist: 1});
b.train('neutral', {dry: 1,wet: 1,moist:1});
b.train('good', {dry: 1,wet: 0,moist: 1});
console.log(b.classify({ dry: 0, wet: 1, moist: 1}));
// good: 0.5, bad: 0.25, neutral: 0.25
But when I can't figure out how to train the data by iterating through data.
I need help to feed the array dynamically as a javascript object.
if you can guarantee the data structure consistency, such as
let data = [key, value, key, value, key, value......]
const data = ['good', { dry: 1, wet: 0, moist:0}, 'neutral', {dry: 1,wet: 1,moist:1}, 'good', {dry: 1,wet: 0,moist: 1}];
// 1: chunk it
const size = 2;
const chunks = [];
while (data.length) {
chunks.push(data.splice(0, size));
}
console.log(chunks);
// 2: loop through your train
let keys = chunks.map(val=>val[0])
let deDupeKeys = [...new Set(keys)]
console.log(keys)
console.log(deDupeKeys)
// var b = new Bayes(deDupeKeys)
chunks.forEach(chunk => {
console.log(chunk[0])
console.log(chunk[1])
// b.train(chunk[0],chunk[1]);
})
Assuming the data array will have the format: data = [category, dataset, category, dataset...], a simple solution would be to loop the data array as follows and train the classifier.
for (let i = 0; i < data.length; i = i + 2) {
console.log("category : ", data[i], "dataset : ", data[i + 1]);
b.train(data[i], data[i + 1]);
}

How to update value in array based on values in another array?

I am having two array like this,
let array1 = [
{
"id": 23,
"name": "Telangana",
}
]
Here i need to update array2 color value inside properties based on array1 numberOfProjects value inside latestMetric. As u can see that in both arrays stateId and id are same.If numberOfProjects value is in the range 1 - 1000. I need to update the color value as 1. then numberOfProjects value is in the range 1000 - 2000. I need to update the color value as 2.so on. I dont know how to achieve this. I tried to map those two arrays and can able to get the ID's.But i dont know how to compare them and update the value . Pleas help me.Thanks in advance
You can do like this
let updatedArr2 = [];
function updateArr2(arr2values, colorValue) {
let updatedProperties = { ...arr2values.properties, color: colorValue };
arr2values.properties = updatedProperties;
updatedArr2.push(arr2values);
}
array2.map(arr2values =>
array1.map(arr1values => {
if (arr2values.properties.stateId === arr1values.latestMetric.stateId) {
if (
arr1values.latestMetric.numberOfProjects >= 1 &&
arr1values.latestMetric.numberOfProjects <= 1000
) {
updateArr2(arr2values, 1);
} else if (
arr2values.latestMetric.numberOfProjects >= 1000 &&
arr2values.latestMetric.numberOfProjects <= 2000
) {
updateArr2(arr2values, 2);
}
}
})
);
console.log(updatedArr2);
You could loop through each object in array1 and then check if there's any object in array2 that matches the stateId, if so, then check the number of projects in the array1 object and change the color of the object in array2 that has the same stateId, something like:
array1.forEach((o) => {
let matches = array2.filter(
(o2) => o2.properties.stateId === o.latestMetric.stateId
);
let projects = o.latestMetric.numberOfProjects;
for (let match of matches) {
if (projects > 1 && projects < 1000) {
match.properties.color = 1;
} else if (projects >= 1000 && projects < 2000) {
match.properties.color = 2;
}
}
});
let array1 = [
{
id: 23,
name: "Telangana",
code: "lnn",
regionId: 1,
isActive: true,
latitude: 17.8495919,
longitude: 79.1151663,
latestMetric: {
stateId: 23,
year: 0,
constructionValueInMn: 84623,
constructionAreaInMnSqft: 32,
numberOfProjects: 406,
noOfCompletedProjects: 19,
noOfOngoingProjects: 387,
noOfUpcomingProjects: 0,
growthRate: 0,
averagePricePerSqftInRs: 0,
totalAreaInMnSqft: 71,
overAllAvgSqft: 0,
eachVariantAvgSqft: 0,
noOfTypeOfVariant: 0,
projectCompletionCycle: 0,
},
createdAt: "2020-04-21T00:35:11.684134",
updatedAt: "2020-04-21T00:35:11.684134",
},
];
let array2 = [
{
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [
[
[77.19721, 28.861519],
[77.203836, 28.86004],
],
],
},
properties: {
cartodb_id: 26,
state_code: 7,
st_nm: "NCT of Delhi",
color: 2,
id: 23,
stateId: 23,
},
},
];
array1.forEach((o) => {
let matches = array2.filter(
(o2) => o2.properties.stateId === o.latestMetric.stateId
);
let projects = o.latestMetric.numberOfProjects;
for (let match of matches) {
if (projects > 1 && projects < 1000) {
match.properties.color = 1;
} else if (projects >= 1000 && projects < 2000) {
match.properties.color = 2;
}
}
});
console.log(array2);
Try this:
array2.map(arr2 => {
//Find to return the position when the id's are the same
const arr1 = array1.find(arr => arr.latestMetric.stateId == arr2.properties.id)
// If find was successful, do this
if (arr1) {
// Destructuring assignment to be easier to compare
const { numberOfProjects } = arr1.latestMetric
if (numberOfProjects >= 1 && numberOfProjects < 1000)
arr2.properties.color = 1
else if (numberOfProjects >= 1000 && numberOfProjects < 2000)
arr2.properties.color = 2
}
})

How to optimize array grouping?

I have an array that is constantly updated and accordingly it is necessary to update its grouping. Example of an array:
[
{
"price": 2419.62,
"amount": 0.0266
},
{
"price": 1927.52,
"amount": 0.0217
},
...
]
I tried different options. At the moment this option is the fastest:
const points = [
{
"price": 2419.62,
"amount": 0.0266
},
{
"price": 1927.52,
"amount": 0.0217
},
...
];
const range = 500;
const spread = 1800;
const countGroup = 250;
const sizeUnitGroup = range / countGroup;
const groups = {};
for (let i = 0; i < countGroup; i++){
groups[i] = [];
try {
points.forEach((item, id) => {
if (item.price > spread + (i*sizeUnitGroup) && item.price <= spread + (i*sizeUnitGroup + sizeUnitGroup)){
groups[i].push(item);
points.splice(id, 1);
}
if (item.price > (spread + (i*sizeUnitGroup + sizeUnitGroup))) throw BreakException;
});
} catch (e) {
}
}
But even so, this function works for too long. Any ideas how this can be optimized?
You could calculate the interval for pushing the value to the wanted slot.
var points = [
{ price: 2419.62, amount: 0.0266 },
{ price: 1927.52, amount: 0.0217 },
{ price: 1800, amount: 0.07 }, // -1 not in result
{ price: 1800.000000000001, amount: 0.07 }, // 0
{ price: 1802, amount: 0.07 }, // 0
],
range = 500,
spread = 1800,
countGroup = 250,
sizeUnitGroup = range / countGroup,
groups = {};
points.forEach((item, id) => {
var i = Math.ceil((item.price - spread- sizeUnitGroup) / sizeUnitGroup);
if (i >= 0 && i < countGroup) {
groups[i] = groups[i] || [];
groups[i].push(item);
}
});
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }

javascript sum array of arrays of objects together

i have an array with the following format
[
[{year:2015, value:23000, value1:1000},{year:2016,value:1000, value1:2000},{year:2017,value:400, value1:3000}],
[{year:2015, value:10000, value1:1000},{year:2016,value:2000, value1:3000},{year:2017,value:500, value1:2000}],
]
i want to sum them together, in this example, i want to have
[{year:2015,value:33000, value1:2000},{year:2016,value:3000, value1:5000},{year:2017,value:900, value1:5000}]
if there are only two arrays in the array but there maybe more arrays in the bigger array. how can i achieve this? currently, i am not able to figure out a good method. Thanks
Ecmascript5 solution:
var arr = [
[{year:2015, value:23000, value1:1000},{year:2016,value:1000, value1:2000},{year:2017,value:400, value1:3000}],
[{year:2015, value:10000, value1:1000},{year:2016,value:2000, value1:3000},{year:2017,value:500, value1:2000}]
],
// summing up values for each `year`
years = arr.reduce(function (r, a) {
a.forEach(function (o) {
y = o.year;
if (Array.isArray(r[y])) {
r[y][0] += o.value;
r[y][1] += o.value1;
} else {
r[y] = [o.value, o.value1];
}
});
return r;
}, {}),
// constructing the resulting array containing objects with `year` totals
result = Object.keys(years).map(function (y) {
return {year: y, value: years[y][0], value1: years[y][1]};
});
console.log(result);
You can loop over the items and add value agains a year number (key) in a map / object.
var data = [
[{year:2015, value:23000},{year:2016,value:1000},{year:2017,value:400}],
[{year:2015, value:10000},{year:2016,value:2000},{year:2017,value:500}],
];
var output = {};
data.forEach(function(item) {
item.forEach(function(obj) {
output[obj.year] = (output[obj.year] || 0) + obj.value;
});
});
output = [output]
console.log(output);
One way is to flat this 2D array first.
var output = {};
var flatArray = data.reduce(function(a, b) {
return a.concat(b);
}, []);
flatArray.forEach(function(obj) {
output[obj.year] = (output[obj.year] || 0) + obj.value;
});
output = [output]
console.log(output);
output: [{
2015: 33000,
2016: 3000,
2017: 900
}]
You could use a hash table for collecting a single year and a nested approach for the arrays with iterating all properties of the given objects.
Then create new properties and add the actual value.
It works with an arbitrary count of properties.
var data = [[{ year: 2015, value: 23000, value1: 1000 }, { year: 2016, value: 1000, value1: 2000 }, { year: 2017, value: 400, value1: 3000 }], [{ year: 2015, value: 10000, value1: 1000 }, { year: 2016, value: 2000, value1: 3000 }, { year: 2017, value: 500, value1: 2000 }]],
result = data.reduce(function (hash) {
return function (r, a) {
a.forEach(function (o) {
if (!hash[o.year]) {
hash[o.year] = { year: o.year };
r.push(hash[o.year]);
}
Object.keys(o).forEach(function (k) {
if (k !== 'year') {
hash[o.year][k] = (hash[o.year][k] || 0) + o[k];
}
});
});
return r;
};
}(Object.create(null)), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Here's a working solution. This will do what you're looking for. Hope it helps!
var arr = [
[{year:2015, value:23000, value1:1000},{year:2016,value:1000, value1:2000},{year:2017,value:400, value1:3000}],
[{year:2015, value:10000, value1:1000},{year:2016,value:2000, value1:3000},{year:2017,value:500, value1:2000}],
]
var result = [];
for(var i = 0; i < arr[0].length; i++){
var count = 0
var count2 = 0;
for(var j = 0; j < arr.length; j++){
var year = arr[j][i].year;
count += arr[j][i].value;
count2 += arr[j][i].value1;
}
result.push({
year: year,
value: count,
value1: count2
});
}
console.log(result);

Categories