I am trying to rewrite an example provided by d3.js. The original code had a function like this:
function interpolateValues(values, year) {
var i = bisect.left(values, year, 0, values.length - 1),
a = values[i];
if (i > 0) {
var b = values[i - 1],
t = (year - a[0]) / (b[0] - a[0]);
return a[1] * (1 - t) + b[1] * t;
}
return a[1];
}
where 'values' is an array which looked like this:
"income":[[1800,359.93],[1820,359.93],[1913,556.12],[1950,3363.02], etc.
however, we store data in a different manner. Unlike the "name":[[year,value]],... format, our data is stored in a different manner, much like a Database (sample data to follow):
{"region":"A","income":1178.0,"lifeExpectancy":788.0,"year":1800.0,"population":1292.0,"country":"Angola"},
{"region":"B","income":847.0,"lifeExpectancy":1183.0,"year":1800.0,"population":803.0,"country":"Benin"},
{"region":"A","income":798.0,"lifeExpectancy":765.0,"year":1801.0,"population":967.0,"country":"Angola"},
{"region":"B","income":1293.0,"lifeExpectancy":1497.0,"year":1801.0,"population":792.0,"country":"Benin"},
I am looking to replace the interpolateValues() function with a different one that goes something like this:
SELECT income FROM dataset WHERE year = 1980
I know this is SQL, I'm just using it to try and get the idea across. Basically you have an array with data, an array with years and I am looking for a function that returns data for a specific year.
The interpolation feature would be nice to have, but it isn't too critical, for now. The problem is that I am just learning to use js, and whatever I tried so far, failed bigtime.
Thanks for your help!
Edit: Some extra information for clarification:
I have this function, which works:
function provideData(year) {
return nations.map(function(d) {
return {
name: d.country,
region: d.region,
income: d.income,
population: d.population,
lifeExpectancy: d.lifeExpectancy
};
});
}
But, as you would expect it returns the full arrays resulting in an output of well over a hundred years' data. Now if only there was a way to return d.income if d.year == year..