-
-
Save ValentinFunk/8f2b536d031ecfab9b661366f518a6c6 to your computer and use it in GitHub Desktop.
Get Exchange Rates from ECB, restricted by date, for each date (uses last rate for unknown days) and export as CSV. JSBin: https://jsbin.com/cetihotuzu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/1.2.2/bluebird.js"></script> | |
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.12.0/axios.min.js"></script> | |
<script src="https://cdn.rawgit.com/mikebald/CSV-Export/672c0158/CSVExport.js"></script> | |
<script id="jsbin-javascript"> | |
let dates = []; | |
let start = moment('2016-08-01'); | |
const data = [{"date":"2016-08-01T22:00:00.000Z","rate":1.1193},{"date":"2016-08-02T22:00:00.000Z","rate":1.12},{"date":"2016-08-03T22:00:00.000Z","rate":1.1136},{"date":"2016-08-04T22:00:00.000Z","rate":1.1156},{"date":"2016-08-07T22:00:00.000Z","rate":1.1087},{"date":"2016-08-08T22:00:00.000Z","rate":1.1078},{"date":"2016-08-09T22:00:00.000Z","rate":1.1184},{"date":"2016-08-10T22:00:00.000Z","rate":1.1153},{"date":"2016-08-11T22:00:00.000Z","rate":1.1158},{"date":"2016-08-14T22:00:00.000Z","rate":1.118},{"date":"2016-08-15T22:00:00.000Z","rate":1.1295},{"date":"2016-08-16T22:00:00.000Z","rate":1.1276},{"date":"2016-08-17T22:00:00.000Z","rate":1.1321},{"date":"2016-08-18T22:00:00.000Z","rate":1.1326},{"date":"2016-08-21T22:00:00.000Z","rate":1.1306},{"date":"2016-08-22T22:00:00.000Z","rate":1.1339},{"date":"2016-08-23T22:00:00.000Z","rate":1.1268},{"date":"2016-08-24T22:00:00.000Z","rate":1.129},{"date":"2016-08-25T22:00:00.000Z","rate":1.129},{"date":"2016-08-28T22:00:00.000Z","rate":1.117},{"date":"2016-08-29T22:00:00.000Z","rate":1.1168},{"date":"2016-08-30T22:00:00.000Z","rate":1.1132},{"date":"2016-08-31T22:00:00.000Z","rate":1.1146},{"date":"2016-09-01T22:00:00.000Z","rate":1.1193},{"date":"2016-09-04T22:00:00.000Z","rate":1.1156},{"date":"2016-09-05T22:00:00.000Z","rate":1.1159},{"date":"2016-09-06T22:00:00.000Z","rate":1.1237},{"date":"2016-09-07T22:00:00.000Z","rate":1.1296},{"date":"2016-09-08T22:00:00.000Z","rate":1.1268},{"date":"2016-09-11T22:00:00.000Z","rate":1.1226},{"date":"2016-09-12T22:00:00.000Z","rate":1.1247},{"date":"2016-09-13T22:00:00.000Z","rate":1.1218},{"date":"2016-09-14T22:00:00.000Z","rate":1.1254},{"date":"2016-09-15T22:00:00.000Z","rate":1.1226},{"date":"2016-09-18T22:00:00.000Z","rate":1.1165},{"date":"2016-09-19T22:00:00.000Z","rate":1.1184},{"date":"2016-09-20T22:00:00.000Z","rate":1.115},{"date":"2016-09-21T22:00:00.000Z","rate":1.1238},{"date":"2016-09-22T22:00:00.000Z","rate":1.1214},{"date":"2016-09-25T22:00:00.000Z","rate":1.1262},{"date":"2016-09-26T22:00:00.000Z","rate":1.122},{"date":"2016-09-27T22:00:00.000Z","rate":1.1225},{"date":"2016-09-28T22:00:00.000Z","rate":1.1221},{"date":"2016-09-29T22:00:00.000Z","rate":1.1161},{"date":"2016-10-02T22:00:00.000Z","rate":1.1236},{"date":"2016-10-03T22:00:00.000Z","rate":1.1161},{"date":"2016-10-04T22:00:00.000Z","rate":1.1211},{"date":"2016-10-05T22:00:00.000Z","rate":1.1185},{"date":"2016-10-06T22:00:00.000Z","rate":1.114},{"date":"2016-10-09T22:00:00.000Z","rate":1.116},{"date":"2016-10-10T22:00:00.000Z","rate":1.1079},{"date":"2016-10-11T22:00:00.000Z","rate":1.102},{"date":"2016-10-12T22:00:00.000Z","rate":1.1038},{"date":"2016-10-13T22:00:00.000Z","rate":1.1002},{"date":"2016-10-16T22:00:00.000Z","rate":1.0994},{"date":"2016-10-17T22:00:00.000Z","rate":1.0993},{"date":"2016-10-18T22:00:00.000Z","rate":1.0979},{"date":"2016-10-19T22:00:00.000Z","rate":1.098},{"date":"2016-10-20T22:00:00.000Z","rate":1.0886},{"date":"2016-10-23T22:00:00.000Z","rate":1.0891},{"date":"2016-10-24T22:00:00.000Z","rate":1.0872},{"date":"2016-10-25T22:00:00.000Z","rate":1.0925},{"date":"2016-10-26T22:00:00.000Z","rate":1.0927},{"date":"2016-10-27T22:00:00.000Z","rate":1.0922},{"date":"2016-10-30T23:00:00.000Z","rate":1.0946},{"date":"2016-10-31T23:00:00.000Z","rate":1.1025},{"date":"2016-11-01T23:00:00.000Z","rate":1.1095},{"date":"2016-11-02T23:00:00.000Z","rate":1.1064},{"date":"2016-11-03T23:00:00.000Z","rate":1.1093},{"date":"2016-11-06T23:00:00.000Z","rate":1.1062},{"date":"2016-11-07T23:00:00.000Z","rate":1.1038},{"date":"2016-11-08T23:00:00.000Z","rate":1.1022},{"date":"2016-11-09T23:00:00.000Z","rate":1.0895},{"date":"2016-11-10T23:00:00.000Z","rate":1.0904},{"date":"2016-11-13T23:00:00.000Z","rate":1.0777},{"date":"2016-11-14T23:00:00.000Z","rate":1.0765},{"date":"2016-11-15T23:00:00.000Z","rate":1.0702},{"date":"2016-11-16T23:00:00.000Z","rate":1.0717},{"date":"2016-11-17T23:00:00.000Z","rate":1.0629},{"date":"2016-11-20T23:00:00.000Z","rate":1.0631},{"date":"2016-11-21T23:00:00.000Z","rate":1.0617},{"date":"2016-11-22T23:00:00.000Z","rate":1.0602},{"date":"2016-11-23T23:00:00.000Z","rate":1.0548},{"date":"2016-11-24T23:00:00.000Z","rate":1.0592},{"date":"2016-11-27T23:00:00.000Z","rate":1.0588},{"date":"2016-11-28T23:00:00.000Z","rate":1.0576},{"date":"2016-11-29T23:00:00.000Z","rate":1.0635},{"date":"2016-11-30T23:00:00.000Z","rate":1.0627},{"date":"2016-12-01T23:00:00.000Z","rate":1.0642},{"date":"2016-12-04T23:00:00.000Z","rate":1.0702},{"date":"2016-12-05T23:00:00.000Z","rate":1.0734},{"date":"2016-12-06T23:00:00.000Z","rate":1.073},{"date":"2016-12-07T23:00:00.000Z","rate":1.0762},{"date":"2016-12-08T23:00:00.000Z","rate":1.0559},{"date":"2016-12-11T23:00:00.000Z","rate":1.0596},{"date":"2016-12-12T23:00:00.000Z","rate":1.061},{"date":"2016-12-13T23:00:00.000Z","rate":1.0644},{"date":"2016-12-14T23:00:00.000Z","rate":1.0419},{"date":"2016-12-15T23:00:00.000Z","rate":1.0439},{"date":"2016-12-18T23:00:00.000Z","rate":1.0422},{"date":"2016-12-19T23:00:00.000Z","rate":1.0364},{"date":"2016-12-20T23:00:00.000Z","rate":1.0421},{"date":"2016-12-21T23:00:00.000Z","rate":1.0444},{"date":"2016-12-22T23:00:00.000Z","rate":1.0446},{"date":"2016-12-26T23:00:00.000Z","rate":1.0445},{"date":"2016-12-27T23:00:00.000Z","rate":1.0401},{"date":"2016-12-28T23:00:00.000Z","rate":1.0453},{"date":"2016-12-29T23:00:00.000Z","rate":1.0541}]; | |
let mapped = data.reduce((acc, cur) => { | |
acc[moment(cur.date).subtract(1, 'd').format('YYYY-MM-DD')] = cur.rate; | |
return acc; | |
}, {}); | |
console.log(mapped) | |
let latestRate = Object.values(mapped)[0]; | |
while(!moment('2017-01-01').isSame(start, 'day')) { | |
let rate; | |
const todaysRate = mapped[start.format('YYYY-MM-DD')]; | |
if (todaysRate) { | |
rate = todaysRate; | |
latestRate = todaysRate; | |
} else { | |
rate = latestRate | |
} | |
dates.push({ | |
date: start.format('YYYY-MM-DD'), | |
rate: rate | |
}); | |
start = start.add(1, 'd'); | |
} | |
console.log(dates); | |
CSVExport(dates) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">let dates = []; | |
let start = moment('2016-08-01'); | |
const data = [{"date":"2016-08-01T22:00:00.000Z","rate":1.1193},{"date":"2016-08-02T22:00:00.000Z","rate":1.12},{"date":"2016-08-03T22:00:00.000Z","rate":1.1136},{"date":"2016-08-04T22:00:00.000Z","rate":1.1156},{"date":"2016-08-07T22:00:00.000Z","rate":1.1087},{"date":"2016-08-08T22:00:00.000Z","rate":1.1078},{"date":"2016-08-09T22:00:00.000Z","rate":1.1184},{"date":"2016-08-10T22:00:00.000Z","rate":1.1153},{"date":"2016-08-11T22:00:00.000Z","rate":1.1158},{"date":"2016-08-14T22:00:00.000Z","rate":1.118},{"date":"2016-08-15T22:00:00.000Z","rate":1.1295},{"date":"2016-08-16T22:00:00.000Z","rate":1.1276},{"date":"2016-08-17T22:00:00.000Z","rate":1.1321},{"date":"2016-08-18T22:00:00.000Z","rate":1.1326},{"date":"2016-08-21T22:00:00.000Z","rate":1.1306},{"date":"2016-08-22T22:00:00.000Z","rate":1.1339},{"date":"2016-08-23T22:00:00.000Z","rate":1.1268},{"date":"2016-08-24T22:00:00.000Z","rate":1.129},{"date":"2016-08-25T22:00:00.000Z","rate":1.129},{"date":"2016-08-28T22:00:00.000Z","rate":1.117},{"date":"2016-08-29T22:00:00.000Z","rate":1.1168},{"date":"2016-08-30T22:00:00.000Z","rate":1.1132},{"date":"2016-08-31T22:00:00.000Z","rate":1.1146},{"date":"2016-09-01T22:00:00.000Z","rate":1.1193},{"date":"2016-09-04T22:00:00.000Z","rate":1.1156},{"date":"2016-09-05T22:00:00.000Z","rate":1.1159},{"date":"2016-09-06T22:00:00.000Z","rate":1.1237},{"date":"2016-09-07T22:00:00.000Z","rate":1.1296},{"date":"2016-09-08T22:00:00.000Z","rate":1.1268},{"date":"2016-09-11T22:00:00.000Z","rate":1.1226},{"date":"2016-09-12T22:00:00.000Z","rate":1.1247},{"date":"2016-09-13T22:00:00.000Z","rate":1.1218},{"date":"2016-09-14T22:00:00.000Z","rate":1.1254},{"date":"2016-09-15T22:00:00.000Z","rate":1.1226},{"date":"2016-09-18T22:00:00.000Z","rate":1.1165},{"date":"2016-09-19T22:00:00.000Z","rate":1.1184},{"date":"2016-09-20T22:00:00.000Z","rate":1.115},{"date":"2016-09-21T22:00:00.000Z","rate":1.1238},{"date":"2016-09-22T22:00:00.000Z","rate":1.1214},{"date":"2016-09-25T22:00:00.000Z","rate":1.1262},{"date":"2016-09-26T22:00:00.000Z","rate":1.122},{"date":"2016-09-27T22:00:00.000Z","rate":1.1225},{"date":"2016-09-28T22:00:00.000Z","rate":1.1221},{"date":"2016-09-29T22:00:00.000Z","rate":1.1161},{"date":"2016-10-02T22:00:00.000Z","rate":1.1236},{"date":"2016-10-03T22:00:00.000Z","rate":1.1161},{"date":"2016-10-04T22:00:00.000Z","rate":1.1211},{"date":"2016-10-05T22:00:00.000Z","rate":1.1185},{"date":"2016-10-06T22:00:00.000Z","rate":1.114},{"date":"2016-10-09T22:00:00.000Z","rate":1.116},{"date":"2016-10-10T22:00:00.000Z","rate":1.1079},{"date":"2016-10-11T22:00:00.000Z","rate":1.102},{"date":"2016-10-12T22:00:00.000Z","rate":1.1038},{"date":"2016-10-13T22:00:00.000Z","rate":1.1002},{"date":"2016-10-16T22:00:00.000Z","rate":1.0994},{"date":"2016-10-17T22:00:00.000Z","rate":1.0993},{"date":"2016-10-18T22:00:00.000Z","rate":1.0979},{"date":"2016-10-19T22:00:00.000Z","rate":1.098},{"date":"2016-10-20T22:00:00.000Z","rate":1.0886},{"date":"2016-10-23T22:00:00.000Z","rate":1.0891},{"date":"2016-10-24T22:00:00.000Z","rate":1.0872},{"date":"2016-10-25T22:00:00.000Z","rate":1.0925},{"date":"2016-10-26T22:00:00.000Z","rate":1.0927},{"date":"2016-10-27T22:00:00.000Z","rate":1.0922},{"date":"2016-10-30T23:00:00.000Z","rate":1.0946},{"date":"2016-10-31T23:00:00.000Z","rate":1.1025},{"date":"2016-11-01T23:00:00.000Z","rate":1.1095},{"date":"2016-11-02T23:00:00.000Z","rate":1.1064},{"date":"2016-11-03T23:00:00.000Z","rate":1.1093},{"date":"2016-11-06T23:00:00.000Z","rate":1.1062},{"date":"2016-11-07T23:00:00.000Z","rate":1.1038},{"date":"2016-11-08T23:00:00.000Z","rate":1.1022},{"date":"2016-11-09T23:00:00.000Z","rate":1.0895},{"date":"2016-11-10T23:00:00.000Z","rate":1.0904},{"date":"2016-11-13T23:00:00.000Z","rate":1.0777},{"date":"2016-11-14T23:00:00.000Z","rate":1.0765},{"date":"2016-11-15T23:00:00.000Z","rate":1.0702},{"date":"2016-11-16T23:00:00.000Z","rate":1.0717},{"date":"2016-11-17T23:00:00.000Z","rate":1.0629},{"date":"2016-11-20T23:00:00.000Z","rate":1.0631},{"date":"2016-11-21T23:00:00.000Z","rate":1.0617},{"date":"2016-11-22T23:00:00.000Z","rate":1.0602},{"date":"2016-11-23T23:00:00.000Z","rate":1.0548},{"date":"2016-11-24T23:00:00.000Z","rate":1.0592},{"date":"2016-11-27T23:00:00.000Z","rate":1.0588},{"date":"2016-11-28T23:00:00.000Z","rate":1.0576},{"date":"2016-11-29T23:00:00.000Z","rate":1.0635},{"date":"2016-11-30T23:00:00.000Z","rate":1.0627},{"date":"2016-12-01T23:00:00.000Z","rate":1.0642},{"date":"2016-12-04T23:00:00.000Z","rate":1.0702},{"date":"2016-12-05T23:00:00.000Z","rate":1.0734},{"date":"2016-12-06T23:00:00.000Z","rate":1.073},{"date":"2016-12-07T23:00:00.000Z","rate":1.0762},{"date":"2016-12-08T23:00:00.000Z","rate":1.0559},{"date":"2016-12-11T23:00:00.000Z","rate":1.0596},{"date":"2016-12-12T23:00:00.000Z","rate":1.061},{"date":"2016-12-13T23:00:00.000Z","rate":1.0644},{"date":"2016-12-14T23:00:00.000Z","rate":1.0419},{"date":"2016-12-15T23:00:00.000Z","rate":1.0439},{"date":"2016-12-18T23:00:00.000Z","rate":1.0422},{"date":"2016-12-19T23:00:00.000Z","rate":1.0364},{"date":"2016-12-20T23:00:00.000Z","rate":1.0421},{"date":"2016-12-21T23:00:00.000Z","rate":1.0444},{"date":"2016-12-22T23:00:00.000Z","rate":1.0446},{"date":"2016-12-26T23:00:00.000Z","rate":1.0445},{"date":"2016-12-27T23:00:00.000Z","rate":1.0401},{"date":"2016-12-28T23:00:00.000Z","rate":1.0453},{"date":"2016-12-29T23:00:00.000Z","rate":1.0541}]; | |
let mapped = data.reduce((acc, cur) => { | |
acc[moment(cur.date).subtract(1, 'd').format('YYYY-MM-DD')] = cur.rate; | |
return acc; | |
}, {}); | |
console.log(mapped) | |
let latestRate = Object.values(mapped)[0]; | |
while(!moment('2017-01-01').isSame(start, 'day')) { | |
let rate; | |
const todaysRate = mapped[start.format('YYYY-MM-DD')]; | |
if (todaysRate) { | |
rate = todaysRate; | |
latestRate = todaysRate; | |
} else { | |
rate = latestRate | |
} | |
dates.push({ | |
date: start.format('YYYY-MM-DD'), | |
rate: rate | |
}); | |
start = start.add(1, 'd'); | |
} | |
console.log(dates); | |
CSVExport(dates)</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dates = []; | |
let start = moment('2016-08-01'); | |
const data = [{"date":"2016-08-01T22:00:00.000Z","rate":1.1193},{"date":"2016-08-02T22:00:00.000Z","rate":1.12},{"date":"2016-08-03T22:00:00.000Z","rate":1.1136},{"date":"2016-08-04T22:00:00.000Z","rate":1.1156},{"date":"2016-08-07T22:00:00.000Z","rate":1.1087},{"date":"2016-08-08T22:00:00.000Z","rate":1.1078},{"date":"2016-08-09T22:00:00.000Z","rate":1.1184},{"date":"2016-08-10T22:00:00.000Z","rate":1.1153},{"date":"2016-08-11T22:00:00.000Z","rate":1.1158},{"date":"2016-08-14T22:00:00.000Z","rate":1.118},{"date":"2016-08-15T22:00:00.000Z","rate":1.1295},{"date":"2016-08-16T22:00:00.000Z","rate":1.1276},{"date":"2016-08-17T22:00:00.000Z","rate":1.1321},{"date":"2016-08-18T22:00:00.000Z","rate":1.1326},{"date":"2016-08-21T22:00:00.000Z","rate":1.1306},{"date":"2016-08-22T22:00:00.000Z","rate":1.1339},{"date":"2016-08-23T22:00:00.000Z","rate":1.1268},{"date":"2016-08-24T22:00:00.000Z","rate":1.129},{"date":"2016-08-25T22:00:00.000Z","rate":1.129},{"date":"2016-08-28T22:00:00.000Z","rate":1.117},{"date":"2016-08-29T22:00:00.000Z","rate":1.1168},{"date":"2016-08-30T22:00:00.000Z","rate":1.1132},{"date":"2016-08-31T22:00:00.000Z","rate":1.1146},{"date":"2016-09-01T22:00:00.000Z","rate":1.1193},{"date":"2016-09-04T22:00:00.000Z","rate":1.1156},{"date":"2016-09-05T22:00:00.000Z","rate":1.1159},{"date":"2016-09-06T22:00:00.000Z","rate":1.1237},{"date":"2016-09-07T22:00:00.000Z","rate":1.1296},{"date":"2016-09-08T22:00:00.000Z","rate":1.1268},{"date":"2016-09-11T22:00:00.000Z","rate":1.1226},{"date":"2016-09-12T22:00:00.000Z","rate":1.1247},{"date":"2016-09-13T22:00:00.000Z","rate":1.1218},{"date":"2016-09-14T22:00:00.000Z","rate":1.1254},{"date":"2016-09-15T22:00:00.000Z","rate":1.1226},{"date":"2016-09-18T22:00:00.000Z","rate":1.1165},{"date":"2016-09-19T22:00:00.000Z","rate":1.1184},{"date":"2016-09-20T22:00:00.000Z","rate":1.115},{"date":"2016-09-21T22:00:00.000Z","rate":1.1238},{"date":"2016-09-22T22:00:00.000Z","rate":1.1214},{"date":"2016-09-25T22:00:00.000Z","rate":1.1262},{"date":"2016-09-26T22:00:00.000Z","rate":1.122},{"date":"2016-09-27T22:00:00.000Z","rate":1.1225},{"date":"2016-09-28T22:00:00.000Z","rate":1.1221},{"date":"2016-09-29T22:00:00.000Z","rate":1.1161},{"date":"2016-10-02T22:00:00.000Z","rate":1.1236},{"date":"2016-10-03T22:00:00.000Z","rate":1.1161},{"date":"2016-10-04T22:00:00.000Z","rate":1.1211},{"date":"2016-10-05T22:00:00.000Z","rate":1.1185},{"date":"2016-10-06T22:00:00.000Z","rate":1.114},{"date":"2016-10-09T22:00:00.000Z","rate":1.116},{"date":"2016-10-10T22:00:00.000Z","rate":1.1079},{"date":"2016-10-11T22:00:00.000Z","rate":1.102},{"date":"2016-10-12T22:00:00.000Z","rate":1.1038},{"date":"2016-10-13T22:00:00.000Z","rate":1.1002},{"date":"2016-10-16T22:00:00.000Z","rate":1.0994},{"date":"2016-10-17T22:00:00.000Z","rate":1.0993},{"date":"2016-10-18T22:00:00.000Z","rate":1.0979},{"date":"2016-10-19T22:00:00.000Z","rate":1.098},{"date":"2016-10-20T22:00:00.000Z","rate":1.0886},{"date":"2016-10-23T22:00:00.000Z","rate":1.0891},{"date":"2016-10-24T22:00:00.000Z","rate":1.0872},{"date":"2016-10-25T22:00:00.000Z","rate":1.0925},{"date":"2016-10-26T22:00:00.000Z","rate":1.0927},{"date":"2016-10-27T22:00:00.000Z","rate":1.0922},{"date":"2016-10-30T23:00:00.000Z","rate":1.0946},{"date":"2016-10-31T23:00:00.000Z","rate":1.1025},{"date":"2016-11-01T23:00:00.000Z","rate":1.1095},{"date":"2016-11-02T23:00:00.000Z","rate":1.1064},{"date":"2016-11-03T23:00:00.000Z","rate":1.1093},{"date":"2016-11-06T23:00:00.000Z","rate":1.1062},{"date":"2016-11-07T23:00:00.000Z","rate":1.1038},{"date":"2016-11-08T23:00:00.000Z","rate":1.1022},{"date":"2016-11-09T23:00:00.000Z","rate":1.0895},{"date":"2016-11-10T23:00:00.000Z","rate":1.0904},{"date":"2016-11-13T23:00:00.000Z","rate":1.0777},{"date":"2016-11-14T23:00:00.000Z","rate":1.0765},{"date":"2016-11-15T23:00:00.000Z","rate":1.0702},{"date":"2016-11-16T23:00:00.000Z","rate":1.0717},{"date":"2016-11-17T23:00:00.000Z","rate":1.0629},{"date":"2016-11-20T23:00:00.000Z","rate":1.0631},{"date":"2016-11-21T23:00:00.000Z","rate":1.0617},{"date":"2016-11-22T23:00:00.000Z","rate":1.0602},{"date":"2016-11-23T23:00:00.000Z","rate":1.0548},{"date":"2016-11-24T23:00:00.000Z","rate":1.0592},{"date":"2016-11-27T23:00:00.000Z","rate":1.0588},{"date":"2016-11-28T23:00:00.000Z","rate":1.0576},{"date":"2016-11-29T23:00:00.000Z","rate":1.0635},{"date":"2016-11-30T23:00:00.000Z","rate":1.0627},{"date":"2016-12-01T23:00:00.000Z","rate":1.0642},{"date":"2016-12-04T23:00:00.000Z","rate":1.0702},{"date":"2016-12-05T23:00:00.000Z","rate":1.0734},{"date":"2016-12-06T23:00:00.000Z","rate":1.073},{"date":"2016-12-07T23:00:00.000Z","rate":1.0762},{"date":"2016-12-08T23:00:00.000Z","rate":1.0559},{"date":"2016-12-11T23:00:00.000Z","rate":1.0596},{"date":"2016-12-12T23:00:00.000Z","rate":1.061},{"date":"2016-12-13T23:00:00.000Z","rate":1.0644},{"date":"2016-12-14T23:00:00.000Z","rate":1.0419},{"date":"2016-12-15T23:00:00.000Z","rate":1.0439},{"date":"2016-12-18T23:00:00.000Z","rate":1.0422},{"date":"2016-12-19T23:00:00.000Z","rate":1.0364},{"date":"2016-12-20T23:00:00.000Z","rate":1.0421},{"date":"2016-12-21T23:00:00.000Z","rate":1.0444},{"date":"2016-12-22T23:00:00.000Z","rate":1.0446},{"date":"2016-12-26T23:00:00.000Z","rate":1.0445},{"date":"2016-12-27T23:00:00.000Z","rate":1.0401},{"date":"2016-12-28T23:00:00.000Z","rate":1.0453},{"date":"2016-12-29T23:00:00.000Z","rate":1.0541}]; | |
let mapped = data.reduce((acc, cur) => { | |
acc[moment(cur.date).subtract(1, 'd').format('YYYY-MM-DD')] = cur.rate; | |
return acc; | |
}, {}); | |
console.log(mapped) | |
let latestRate = Object.values(mapped)[0]; | |
while(!moment('2017-01-01').isSame(start, 'day')) { | |
let rate; | |
const todaysRate = mapped[start.format('YYYY-MM-DD')]; | |
if (todaysRate) { | |
rate = todaysRate; | |
latestRate = todaysRate; | |
} else { | |
rate = latestRate | |
} | |
dates.push({ | |
date: start.format('YYYY-MM-DD'), | |
rate: rate | |
}); | |
start = start.add(1, 'd'); | |
} | |
console.log(dates); | |
CSVExport(dates) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment