1

I'm referring to this answer.

Why does Code Snippet 2 not return the same as Code Snippet 1?

Code Snippet 1:

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;
}, []);

Code Snippet 2:

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['eventTitle'] = e.getTitle();
 ar['eventId'] = id;
 ar['startDate'] = e.getAllDayStartDate();
 ar['endDate'] = e.getAllDayEndDate();
 }
 return ar;
}, []);

EDIT:

Why does Code Snippet 3 behave like expected (adding one object per event series) while Code Snippet 4 doesn't?

Code Snippet 3:

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});
 }
 return ar;
}, []);

Code Snippet 4:

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()});
 }
 return ar;
}, []);
asked Mar 18, 2020 at 17:59

3 Answers 3

2

ar.push({/*...*/}) creates an object and pushes that object into the array.

ar['eventTitle'] = e.getTitle(); and such create properties on the array with those names (overwriting the previous values each time). They don't put entries in arrays.

The version with push is correct if your goal is to put objects in the array.

It may seem odd to have arbitrary properties on arrays, but standard arrays are objects, so you can add properties to them.

If you wanted to add to the array with assignment, you'd do it like this:

ar[ar.length] = {eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()};

That's effectively what push does.

answered Mar 18, 2020 at 18:03
2
  • Thank you! I need to use variable values (e.g. e.getTitle().substr(0, e.getTitle().indexOf('something'))) as keys. How to achieve that? Commented Mar 18, 2020 at 18:12
  • 1
    @Ben - Like this. :-) Commented Mar 18, 2020 at 19:26
1

First sample code uses push, which is doing adding one more object to array ar.

Second sampel code using bracket notation, which is changing the values in the object ar.

answered Mar 18, 2020 at 18:06
1

In the first example the reduce function is pushing data into the aggregated array.

ar.push({...})

In the second example the aggregation sets values on the array as keys, arrays === objects in JS.

ar['key-name'] = whatever
Ben
1,6383 gold badges17 silver badges25 bronze badges
answered Mar 18, 2020 at 18:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.