Combine filter() and startsWith() to filter array

array filter
typescript array filter
filter startswith in r
array prototype filter call
js filter array starts with
array filter start with
typescript array starts with
string filter javascript

Suppose I have an array constant as follows:

const people = [
      { first: 'John', last: 'Doe', year: 1991, month: 6 },
      { first: 'Jane', last: 'Doe', year: 1990, month: 9 },
      { first: 'Jahn', last: 'Deo', year: 1986, month: 1 },
      { first: 'Jone', last: 'Deo', year: 1992, month: 11 },
      { first: 'Jhan', last: 'Doe', year: 1989, month: 4 },
      { first: 'Jeon', last: 'Doe', year: 1992, month: 2 },
      { first: 'Janh', last: 'Edo', year: 1984, month: 7 },
      { first: 'Jean', last: 'Edo', year: 1981, month: 8},
];

And I want to return a value of everyone born in the 80s.

My current working function for achieving this is:

const eighty = people.filter(person=> {
    if (person.year >= 1980 && person.year <= 1989) {
        return true;
    }
});

My Question: Is it possible to use startsWith() along with filter() to replace:

if (person.year >= 1980 && person.year <= 1989) {
    return true;
}

with startsWith('198') instead?

If yes, what would be the right way to do it?

You can do

people.filter(person => String(person.year).startsWith('198'))

const people = [
      { first: 'John', last: 'Doe', year: 1991, month: 6 },
      { first: 'Jane', last: 'Doe', year: 1990, month: 9 },
      { first: 'Jahn', last: 'Deo', year: 1986, month: 1 },
      { first: 'Jone', last: 'Deo', year: 1992, month: 11 },
      { first: 'Jhan', last: 'Doe', year: 1989, month: 4 },
      { first: 'Jeon', last: 'Doe', year: 1992, month: 2 },
      { first: 'Janh', last: 'Edo', year: 1984, month: 7 },
      { first: 'Jean', last: 'Edo', year: 1981, month: 8},
];

var filtered = people.filter(p => String(p.year).startsWith('198'));

console.log(filtered);

Filters an array of objects with multiple match-criteria. · GitHub, `function filterArray<TInput = any>(array: TInput[], filters: IFilters) => TInput[]` indexOf() returns -1 if the element is not present in the array, so if I apply ~-1 , I will get The goal would be to get all objects which have a color that starts with "​Bl". Combining filter() with reduce(), you can remove values from an array, using the results to compute some value. Get some practice with this combination.

This isn't quite what you asked, sorry, but if you're interested in solving the problem in one operation rather than using startsWith specifically, you can do it numerically...

Math.floor(person.year / 10) === 198

It may be slighly more efficient as no string conversion and doesn't have the problems of other strings starting the same way matching.

Python : filter() function, In this article we will discuss when & how to use python's filter() function with lambda Let's pass a lambda function to filter() for selecting strings with length 2 only from the list i.e. Filter an array in Python using filter() Sort a Dictionary by key or Value · Merge two or more Dictionaries · Read More  Combining filter() and map() 4:13 with Joel Kraft. You can combine filter() and map() to clean an array of unwanted values before transforming the leftover items

Yes you can:

people.filter(person => String(person.year).startsWith('198'));

However, you probably don't want to because you could run into weird things where the year isn't valid (like if it was 19812).

Instead, you'd be better off using regex:

people.filter(person => /^198\d$/.test(person.year));

This will only match the years in the 1980s. You also don't have to do the extra cast, so its a little cleaner too.

Understanding the filter() Method in JavaScript - Joe Cardillo, Today's question forced me to dig deeper into the filter() method and spread operator. A simple way to combine two strings is to use the addition operator: += of what filter() is doing, let's say we wanted to filter all the x's into a new array. I need some help with a filter on a spreadsheet. This follows a 'list rows present in a table' action. The goal of the flow is to update or create items in a sharepoint list. One of the columns is called project phase. I want to filter out all projects where the phase is either empty or closed.

Java 8 Predicate Chain, First, let's see how to use a simple Predicate to filter a List of names: List<String​> names = Arrays. startsWith( "A" ) && name.length() < 5 ) We'll combine Predicates using the methods Predicate.and(), Predicate.or(), and  Filter query=startswith(fieldname,’startvalue’) Filter array. These are very much similar to what we get in D365 native workflows except for puttin the value

LINQ filter: How to filter a list using another list with LINQ, First, I added a filter for the selected genres (a string array) in Figure 2. The GetMovies() method returns a list of movies. In this case, it's a just a  Hi, I know this an old post but I'm having the same problems, the Search function always overrides the Filter results. I have tried all the suggestions in posts but always the same, the results are correctly filtered by the Dropdown1 & Dropdown1_2 selections but if I search via TextSearchBox1.Text the results are from the entire database and not filtered.

Python - Filter list elements starting with given Prefix, Method #1 : Using list comprehension + startswith() In this method, we use list comprehension for traversal logic and the startswith method to filter out all the  How can I combine multiple Groups.startsWith (for example) calls into one claim list? I have a claim (Value Type = Expression) in which I would like to combine several sets of matches. Each of these works independently:

Comments
  • youd have to convert it to a string first...but it would also then cover a year of 19801 for example which is invalid
  • Instead of startsWith, you could avoid the mentioned edge case by matching a regex instead. Maybe /^198\d$/.test(String(year))
  • You don’t need this if statement. Just return the condition: people.filter((person) => person.year >= 1980 && person.year <= 1989) or use destructuring: people.filter(({year}) => year >= 1980 && year <= 1989).
  • return Math.floor(person.year / 10) === 198
  • My basic problem is that these alternate solutions save you like half a line of code, and in exchange it's either a massive overhead (converting to string + checking a substring) compared to a simple value check, or in the case of Math.floor(), it's really unintuitive to read. I'd recommend you go with @Xufox's suggestion and just return the boolean expression instead of using an if branch.
  • Is it p => String(p.year) or p => String(people.year)?
  • sorry for that mistake... p.year or person.year whatever you are using
  • That would help avoid the edge case of it being larger than 1989.
  • +1 This is definitely more reliable and straightforward and I think I'll definitely go with this when working with filtering arrays @brabster, but Zohaib's answer showed me a way to combine the two methods as I initially wanted to know so I'm giving him the tick man. Cheers.
  • Good call pal happy to help ;)
  • +1 Thank you @samanime! Will be using Math.floor or regex instead of startsWith() for filtering numbered arrays.