1

i am trying to get specific week(such as saturday) days collection from my mongodb. here is the example DB collection

var array2 = [
{
"_id": "1",
"start": "2016-08-23T16:00:00.000Z",
},
{
"_id": "2",
"start": "2016-08-26T21:00:00.000Z",
},
{
"_id": "3",
"start": "2016-08-23T16:10:00.000Z",
}];

javascript week reference to get Saturday entry i run a map, and here is the code

array2.map(function(object){ 
 var newObject = {};
 var nedatee = new Date(object.start);
 var weekday = nedatee.getDay();
 if (weekday===6) {
     newObject['start'] = true;
 }
 return newObject;
});

the output is

[ {}, {'start':true}, {}]

i dont want the empty object in my array, and i dont want to manually remove it, is there any smarted way? how can i make good use of reduceRight ? all i want is

[{'start':true}]

javascript map reference if you need

nur farazi
  • 1,197
  • 12
  • 32
  • did u try using `mapReduce` on db itself – Naeem Shaikh Aug 22 '16 at 05:27
  • Use [*filter*](http://ecma-international.org/ecma-262/7.0/index.html#sec-array.prototype.filter), which creates a new array based on the elements that return *true* from the callback function. Or if you want to modify the array itself, use [*reduceRight*](http://ecma-international.org/ecma-262/7.0/index.html#sec-array.prototype.reduceright) and splice the failures. – RobG Aug 22 '16 at 05:32
  • Possible duplicate of [Removing elements with Array.map in JavaScript](http://stackoverflow.com/questions/9289/removing-elements-with-array-map-in-javascript) – Mike Aug 22 '16 at 05:45

3 Answers3

2

Have you considered using javascript some? Your code would then be

var hasSaturdays = array2.some(function isSaturday(object){ 
 var nedatee = new Date(object.start);
 return nedatee.getDay() === 6;
});

Then if hasSaturdays is true then you can return [{'start':true}]

Please note that it is only IE 9+

derp
  • 2,300
  • 13
  • 20
  • 2
    Note that this parses the date as UTC but does the comparison as local, so it will give different results in different time zones. – RobG Aug 22 '16 at 05:44
2

You could use reduceRight and splice the failures:

var array2 = [
{
"_id": "1",
"start": "2016-08-23T16:00:00.000Z",
},
{
"_id": "2",
"start": "2016-08-26T21:00:00.000Z",
},
{
"_id": "3",
"start": "2016-08-23T16:10:00.000Z",
}];

array2.reduceRight(function(acc,v,i) {
  if (new Date(v.start).getUTCDay() != 6){
    array2.splice(i,1);
  }
},null)

console.log(array2);

Note that the date will be parsed as UTC, so 2016-08-26T21:00:00.000Z will be Saturday in any timezone that is UTC+0300 or more, hence use of getUTCDay (i.e. none of the days is Saturday in the GMT time zone).

Also, you should not really rely on parsing any date string with the Date constructor. You should use a parser and provide the format, see Chrome can't parse date but IE can.

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209
2

Using UTC time zone or not this is a simple filtering work and there is Array.prototype.filter() just for this job.

var array2 = [
{
"_id": "1",
"start": "2016-08-23T16:00:00.000Z",
},
{
"_id": "2",
"start": "2016-08-26T21:00:00.000Z",
},
{
"_id": "3",
"start": "2016-08-23T16:10:00.000Z",
}],
saturdays = array2.filter(date => (new Date(date.start)).getDay() === 6)
                  .map(date => ({start:true}));
console.log(saturdays);
Redu
  • 25,060
  • 6
  • 56
  • 76