I want to iterate an object's properties, and push the value of each property to an array sharing the property's name.
How can I reference the array name dynamically using the name of a property?
var obj = {"timeslot": "6am-7am", "Monday": "5", "Tuesday": "9"};
var timeslot = [];
var monday = [];
var tuesday = [];
Object.keys(obj).forEach(function(key) {
console.log(key); // Returns "timeslot", "Monday", and "Tuesday" respectively
console.log(obj[key]); // Returns "6am-7am", "5" and "9" respectively
});
The result I want is three arrays, like so:
timeslot = ["6am-7am"];
monday = ["5"];
tuesday = ["9"];
window[key] = obj[key]in your forEach loop (that's for global var in browser)