Suppose I have a hash h and an array a like this:
h = {'data': {}};
arr = ['2017-01-01', '2017-02-01','2017-03-01', '2017-04-01', ....];
What is the most concise and efficient way to construct a default hash table as below in JavaScript:
// desired outcome for 'h'
h = {
'data': {
'2017-01-01': 0,
'2017-02-01': 0,
'2017-03-01': 0,
'2017-04-01': 0,
//...there can be more date values here
}
}
I have implemented the solution below, but would like to know if there is more JavaScript-y (and hopefully more efficient) way to accomplish below:
arr.forEach(function(a) {
h['data'][a] = 0;
});
Thanks in advance for your suggestion/answers!
arr.forEach(e => h.data[e] = 0 )