alertwera.blogg.se

Javascript find function
Javascript find function










javascript find function

JavaScript is one of the most used languages when it comes to building web applications as it allows developers to wrap HTML and CSS code in it to make web apps interactive. If you are a beginner and want to learn JavaScript then do check out the course on Codedamn. Now, let's say that we want to find all students that did a math exam and had a score of 70 or more, we could do it like this: let res = students.filter(student => student.subject = 'math' & student.score >= 70) īut what if in the future we need modify our code to allow us to filter by more attributes? If we keep doing it in a single line, this one-liner can start to get long and ugly.Hey readers, in this article we will be discussing how to use the find method in JavaScript in a step-wise, efficient manner.

javascript find function

You can also pass a function to the filter function: function isEven( value)

javascript find function

result => Įxample 2: Given an array of numbers, return only the even numbers let numbers = Let result = fruits.filter(fruit => fruit.length > 6) This is achieved very easily: let fruits = Let's check out some very basic usages of the filter function:Įxample 1: Given an array of strings, filter the items with length > 6. DISCLAIMER: I do realize that this post is very similar to the official documentation for the filter function, the purpose of this post is to be a complement to the other post I wrote covering the "find" function. The filter() function, on the other hand, as the name also suggests, is used to filter data, which means that given an array of data, you can filter the data based on any criteria that you can apply to the data in the array, and get a filtered array as the result. To my surprise, the post got a lot of attention, some people even commented about using the filter function for achieving the same result, but the point is that find is the function most indicated for when you - as the name suggests - needs to find a specific element inside an array, and that's why it always returns a single result. Some time ago I wrote this post talking about how to find an object inside an array by one or more of its properties and I talked about using the find function for it, along with other ways to achieving the same.












Javascript find function