Filter array using forEach

array filter
javascript filter array multiple values
javascript foreach filter
foreach javascript
javascript array.filter multiple arguments
foreach filter java
javascript map
reduce vs foreach performance

I got an array of dates, where I want to filter out specific days. That's my previous solution, which works fine:

var available = dates.filter(function(e) {
  return (
    e.getDay() != 0 && // sunday
    e.getDay() != 6 && // saturday
    e.getDay() != 2 && // tuesday
    e.getDay() != 3 && // wednesday
  );
});

Now, I want to make this dynamic. So I got an array like this:

var unavailable = [0, 2, 3, 6]

And then I try to filter out those days like this:

unavailable.forEach(function(x){
  available = dates.filter(function(e, index){
    return e.getDay() != x;
  });
});

That doesn't appear to be working tho. What did I do wrong and how can I get this to work? Thanks in advance.

You need to swith the order of comparing and return the result of the check. In this case you need Array#every instead of Array#forEach, because you need a result for the filtering.

available = dates.filter(function(e, index) {
    return unavailable.every(function(x) {
        return e.getDay() != x;
    });
});

The same with Array#some and a negation of the returned result and a negated comparison.

available = dates.filter(function(e, index) {
    return !unavailable.some(function(x) {
        return e.getDay() === x;
    });
});

A shorter approach.

var unavailable = [0, 2, 3, 6]
    available = dates.filter(e => !unavailable.includes(e.getDay()));

Why and when to use forEach, map, filter, reduce, and find in , forEach() , .map() , .filter() , .reduce() and .find() on arrays in JavaScript. I thought it To me, the most compelling case for using .forEach() in� Many posts discuss how to use .forEach(), .map(), .filter(), .reduce() and .find() on arrays in JavaScript. I thought it would be useful to provide an explanation of when to use the common array…

No need to use forEach use filter and includes

var unavailable = [0, 2, 3, 6]
var dates = ['1-1-2019', '1-2-2019', '1-3-2019', '1-4-2019',' 1-5-2019', '1-6-2019', '1-7-2019', '1-8-2019',' 1-9-2019'];
workingDays = dates.filter(e => {
return !unavailable.includes(new Date(e).getDate())})
console.log(workingDays)

Javascript how to filter an array using forEach() inside filter(), You can iterate the fields using Array#some , and if one of them is equal to value return the item : const array_one = [ {id: 1, code: 'ABC123',� That means, the filter does not change or update the existing array, it gives the new filtered array every time. Difference between forEach() and filter() is that forEach() iterates the array and executes the callback but filter executes the callback and check its return value and on basis of that return value it decided what should be put

If you are not rigid to create an array and is not going to be used anywhere else in your code then no need to.

dates.filter(e => e.getDay().toString().match(/0|6|2|3/));

forEach(), .map(), .filter() . What's the difference?, forEach() , is used to execute the same code on every element in an array but does not change the array and it returns undefined. Example: In the� In this tutorial, we will learn how to use Stream.filter() and Stream.forEach() method with an example. Java stream provides a filter() method to filter stream elements on the basis of a given predicate. This method takes a predicate as an argument and returns a stream consisting of resulted elements.

You can try the following:

available = dates.filter(function(e) {
  return unavailable.indexOf(e.getDay()) === -1 ? true : false;
})

The problem with your code is that forEach rewrites the value of available every iteration, and your program adds difficulty to understand.

Array Methods Explained : Filter vs Map vs Reduce vs Foreach, Filter let you provide a callback for every element and returns a filtered array. The main difference between forEach and filter is that forEach just loop over the array and executes the callback but filter executes the callback and check its return value. forEach() executes the callback function once for each array element; unlike map() or reduce() it always returns the value undefined and is not chainable. The typical use case is to execute side effects at the end of a chain.

I got the right answer for you.

let unavailable = [0, 2, 3, 6];
available = dates.filter(e => !unavailable.includes(e.getDay());

The problem with your code is that for each element in unavailable array, you are resetting available.

Write JavaScript loops using map, filter, reduce and find, Loops are generally used, in any programming language, to perform operations on arrays: given an array you can iterate over its elements and� The filter() method creates an array filled with all array elements that pass a test (provided as a function). Note: filter() does not execute the function for array elements without values. Note: filter() does not change the original array.

Array.prototype.forEach(), The following example illustrates an alternative approach, using forEach() . The following code logs a line for each element in an array: function� array.every() doesn’t only make the code shorter. It is also optimal, because .every() method breaks iterating after finding the first odd number.. 8. Conclusion. array.forEach(callback) method is an efficient way to iterate over all array items.

Array Iteration: 8 Methods, Learn eight methods to iterate through an array in JavaScript! Methods include: forEach, map Duration: 6:22 Posted: Mar 10, 2017 Perhaps I'm simply having trouble understanding how php handles arrays. I'm trying to print out an array using a foreach loop. All I can seem to get out of it is the word "Array".

JavaScript Array Filter: Filtering Array Elements Based on a Test , This tutorial shows you how to use the JavaScript array filter method to filter you typically loop over the array elements using a for loop and test if the value of the of the Array object such as every(), some(), map() and forEach(), the callback� The foreach statement is known to be a quicker alternative than using the ForEach-Object cmdlet. The ForEach-Object CmdLet If foreach is a statement and can only be used in a single way, ForEach-Object is a cmdlet with parameters that can be employed in a lot of different ways.

Comments
  • whats dates in the code?
  • You have to do it the other way round: dates.filter(function() { /* check if .getDay() is part of the array stored in unavailable */ })
  • What is dates array
  • @the_ultimate_developer I didn't include the date array in my question, since it's just an array generated like this [new Date("2019-03-07"), new Date("2019-03-09"), ...] and then turns to [Mon Apr 01 2019 02:00:00 GMT+0200, Tue Apr 02 2019 02:00:00 GMT+0200, ...]
  • Thanks for your answer! I accepted Ninas answer tho, because it's more detailed. +1 tho :)