I have a months array that has 12 dynamically created objects for the next 12 months
months = [
{2020-02: Array(0)}
{2020-03: Array(0)}
{2020-04: Array(0)}
{2020-05: Array(0)}
{2020-06: Array(0)}
{2020-07: Array(0)}
{2020-08: Array(0)}
{2020-09: Array(0)}
{2020-10: Array(0)}
{2020-11: Array(0)}
{2020-12: Array(0)}
{2021-01: Array(0)}
];
I want to insert an object into the object's array based on a dynamic key that is based on another array (source data).
I loop through the source data and each object in the source data's array has a key of media_date and value of 2020-02;
I then create an object to insert into the months array
var person = new Object();
person.title = 'job title';
person.name = 'cool name';
I want to insert the person object into the months array into the object with the key of 2020-02
I tried something like:
$.each(source,function(i,data){
var person = new Object();
person.title = data.job_title;
person.name = data.name;
var key = data.monthyear (2020-02)
months.[key].push(person);
});
But I get
TypeError: Cannot read property 'push' of undefined at...
Thanks for any help you can give!
months.[key]?months[key].push(person);, you don't need the '.' (dot) between months and your keymonths[key] = person?