0

I'm referring to this answer again.

var firstEvents = events.reduce(function(ar, e) {
  var id = e.getId();
  if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
    ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
  }
  return ar;
}, []);

What do I have to change to get an array with the event titles (Strings) as keys and the start dates (Date objects) as values so I can retrieve a certain start date (Date object) via firstEvents['some event title']?

EDIT:

Current Ouput:

firstEvents = [{eventTitle=Event title 1, [email protected], startDate=Sun Mar 18 00:00:00 GMT+01:00 2018, endDate=Mon Mar 19 00:00:00 GMT+01:00 2018},
               {eventTitle=Event title 2, [email protected], startDate=Tue Mar 19 00:00:00 GMT+01:00 2019, endDate=Wed Mar 20 00:00:00 GMT+01:00 2019},
               {eventTitle=Event title 3, [email protected], startDate=Fri Mar 20 00:00:00 GMT+01:00 2020, endDate=Sat Mar 21 00:00:00 GMT+01:00 2020}]

Needed Output (Pseudo):

firstEvents = ['Event title 1' => Sun Mar 18 00:00:00 GMT+01:00 2018,
               'Event title 2' => Tue Mar 19 00:00:00 GMT+01:00 2019,
               'Event title 3' => Fri Mar 20 00:00:00 GMT+01:00 2020]
5
  • 1
    JS doesn't have associative arrays. It uses objects for that. Commented Mar 20, 2020 at 16:53
  • 1
    JavaScript doesn't have real associative arrays. You can build a plain object, but you can't use .push() etc. Commented Mar 20, 2020 at 16:53
  • So what would you suggest? @palaѕн What I need is an array from which I can retrieve a certain start date to a given event title without looping through the array. Commented Mar 20, 2020 at 17:25
  • Please post what data you are getting right now using the above code and what it should look like in your post. You don't need to post an array of 20+ objects but please post 4-5 objects, so that users can understand the patter you are looking for. Commented Mar 20, 2020 at 17:28
  • @palaѕн Did it. Commented Mar 20, 2020 at 18:08

1 Answer 1

1

Do not use push, but set to object with key.

ar = {}; // You may need to change source parameter too
// you cannot change input Array [] to Object {} type inside function
// you can get Array and return Object, but source variable will not change
ar[e.getTitle()] = e.getAllDayStartDate();

Or using some demo data:

var ar = [
{
    eventTitle: 'one',
    eventId: '#1',
    startDate: new Date(),
    endDate: new Date()
},
{
    eventTitle: 'two',
    eventId: 'secondId',
    startDate: new Date(),
    endDate: new Date()
}];
var retVal = {};
for (var i of ar) {
    retVal[i.eventId] = i;
}
console.log(JSON.stringify(retVal, null, 2));
console.log(retVal['#1']);
console.log(retVal.secondId);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.