JavaScript Problem: Comparing Two Arrays without Loops

  Рет қаралды 16,215

All Things JavaScript, LLC

All Things JavaScript, LLC

Күн бұрын

We are going to tackle another JavaScript problem in this tutorial. The specific problem is how one can compare two arrays without using any loops.
Would you like to help keep this channel going?
www.patreon.co...
Earn SCRIPT: app.tryroll.co...
Unlimited access to EVERY course for one low price: allthingsjavas...
Access to EVERY course via subscription (get 2 months free): www.skillshare...
Courses offered on Udemy at a discount (access from my site): allthingsjavasc...
Tutorials referred to in this video:
JavaScript Problem Playlist: • JavaScript Problems
ES5 Methods Map and Reduce: • Getting Comfortable wi...
Need Help? pensight.com/x...
For more resources on JavaScript:
www.allthingsja...
#javascript #AllThingsJavaScriptLLC

Пікірлер: 35
@amatiasq
@amatiasq 3 жыл бұрын
"Comparing Two Arrays without Loops", then proceeds to nest a `.reduce()` inside a `.map()`...
@seb.z.g
@seb.z.g Жыл бұрын
😂
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
So grateful for all of you tuning in each week and for your feedback and encouragement. Glad you are all trying to improve your JavaScript skills! #GiveThanks
@tirmey
@tirmey 3 жыл бұрын
Mapping over an array is kind of intrinsically lopping over it, no? nice solution, anyway!
@trusabaka9747
@trusabaka9747 3 жыл бұрын
My thoughts exactly.
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
Yes, all those methods iterate over an array.
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
The reason to gravitate toward these methods is that the looping is abstracted away and our code is more declarative, which is easier to reason about.
@user-zb5jp4ti1d
@user-zb5jp4ti1d 3 жыл бұрын
Thanks Steve for going to such great lengths to answer the query... you are a legend sir :)
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
My pleasure!
@magnusfohlstrom
@magnusfohlstrom 3 жыл бұрын
Without reduce... function compareArray( searchArray, targetArray ) { return searchArray.map(curr => ({[curr]: targetArray.filter(check => curr === check).length})); }
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
Love this one too!
@MuhammadAdnan2.0
@MuhammadAdnan2.0 3 жыл бұрын
Have to practice this two time arrays comparing.... 😃
@lidwinguillermogascagarcia439
@lidwinguillermogascagarcia439 Жыл бұрын
I just started learning js, and to acomplish my project, I need to compare two arrays of food recipes, one is the recipe, and the other array is the ingredients the user have, all I need to do is to check if the users ingredients array contains the ingredients from the recipe array, if true, show a picture of the recipe (trying to make an app that will show me what dishes I could cook with my ingredients).
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Great task to be working on as you are learning. Best way to learn!
@magnusfohlstrom
@magnusfohlstrom 3 жыл бұрын
returns from one line... function compareArray( searchArray, targetArray ) { return searchArray.reduce((obj,curr) => [...obj, {[curr]: targetArray.filter(check => curr === check).length}], []); }
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
Nice approach! I love it!
@Charlie_904
@Charlie_904 3 жыл бұрын
i was under the impression that those higher order functions map, filter, reduce etc.. all had built in loops to them.
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
Yes, they all iterate over values. When I say without loops I mean structures that are called loops: for, while etc.
@Charlie_904
@Charlie_904 3 жыл бұрын
@@AllThingsJavaScript ahh, gotcha. no problem, love the stuff
@zhasan66
@zhasan66 3 жыл бұрын
Very helpful video tutorial.
@cpu1001
@cpu1001 3 жыл бұрын
Awesome video, thanks for sharing!
@sajeersayed2008
@sajeersayed2008 Жыл бұрын
Perfect
@ScottL888
@ScottL888 3 жыл бұрын
Nice tutorial, thanks. IMO, it would be more helpful and relevant if you were to use arrow functions.
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
Courses discounts available on my site: allthingsjavascript.com/courses.htm JavaScript Problem Playlist: kzbin.info/aero/PLTo9PCskHpbGawvd3GNAR7mfeFoM61v3y
@hamidrezaakbarnezhad2018
@hamidrezaakbarnezhad2018 3 жыл бұрын
so pretty really. I like that. Please solve problems that contains array of objects and operate on each them... and compare solutions in performance and that what is best solution. Tnks
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
What kind of processing are you thinking for each object?
@hamidrezaakbarnezhad2018
@hamidrezaakbarnezhad2018 3 жыл бұрын
@@AllThingsJavaScript Operate on heavy arrays of object... For example all sort algorithm on them... Boble sort and ... To sort this array of object. Or for example sort this array of object base on 3 fields like name(asc), age(desc) and score... I want to improve myselt in work with arrays and objects in js. Tnks
@Well_This_Guy_Says
@Well_This_Guy_Says 3 жыл бұрын
Wouldn't JSON.parse(JSON.stringify) work for this? I use it for comparing an array of objects
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
If you were just comparing if they were equal to one another, yes you could use that.
@TheSwagStatus
@TheSwagStatus 3 жыл бұрын
wordList.filter( conditional checking for === with word in wordCheck).length
@AllThingsJavaScript
@AllThingsJavaScript 3 жыл бұрын
Great way to get the count!
@hammadrana364
@hammadrana364 Жыл бұрын
map also a loopp
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
I get what you are saying, but technically a loop is a control structure. You may not visit every element depending on how you set up the control structure. Map will iterate over every element. This difference is why it is normally not considered a loop.
@MiroslavPopov
@MiroslavPopov 2 жыл бұрын
Hahahah. No loop but map and reduce :)))). Dislike.
@magnusfohlstrom
@magnusfohlstrom 3 жыл бұрын
.map .filter .reduce internally they use loop technically. function compareArray( searchArray, targetArray ) { let objArray = [], searchArrayLen = searchArray.length, targetArrayLen = targetArray.length, tot = 0, count = (search, controller = 0) => { tot = targetArray[controller] === search ? tot + 1 : tot; ++controller !== targetArrayLen && count(search, controller); }; (function fill(controller = 0) { tot = 0; count(searchArray[controller]); objArray[controller] = {[searchArray[controller]]: tot}; ++controller !== searchArrayLen && fill(controller); })(); return objArray; };
Using a Double Sort on an Array of Objects
12:28
All Things JavaScript, LLC
Рет қаралды 8 М.
JavaScript Tip: 7 Ways to Iterate Over an Array
15:11
All Things JavaScript, LLC
Рет қаралды 9 М.
Worst flight ever
00:55
Adam W
Рет қаралды 22 МЛН
SCHOOLBOY. Мама флексит 🫣👩🏻
00:41
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 7 МЛН
Ozoda - Lada (Official Music Video)
06:07
Ozoda
Рет қаралды 7 МЛН
JavaScript Problem: Checking if 2 Objects have the Same Data
14:32
All Things JavaScript, LLC
Рет қаралды 6 М.
JavaScript Problem: Searching an Array for a Value
13:43
All Things JavaScript, LLC
Рет қаралды 38 М.
JavaScript Tip: Using Promise.all with an async Function
7:39
All Things JavaScript, LLC
Рет қаралды 10 М.
JS Tutorial: Find if Two Object Values are Equal to Each Other
14:24
Comparing values of two arrays in JavaScript
13:45
Strictly Web Dev
Рет қаралды 1,2 М.
JavaScript Question: What is a Decorator Function?
9:32
All Things JavaScript, LLC
Рет қаралды 7 М.
JavaScript Question: How Do I Find and Extract a Value from an Array?
10:11
All Things JavaScript, LLC
Рет қаралды 9 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 631 М.
10 MUST KNOW Array Manipulation Methods in JavaScript
17:04
Koding 101
Рет қаралды 1 М.
Worst flight ever
00:55
Adam W
Рет қаралды 22 МЛН