JavaScript Problem: Comparing Two Arrays without Loops

preview_player
Показать описание
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?

Tutorials referred to in this video:

For more resources on JavaScript:

#javascript #AllThingsJavaScriptLLC
Рекомендации по теме
Комментарии
Автор

"Comparing Two Arrays without Loops", then proceeds to nest a `.reduce()` inside a `.map()`...

amatiasq
Автор

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

AllThingsJavaScript
Автор

Mapping over an array is kind of intrinsically lopping over it, no? nice solution, anyway!

tirmey
Автор

Thanks Steve for going to such great lengths to answer the query... you are a legend sir :)

zbjptid
Автор

Without reduce...
function compareArray( searchArray, targetArray ) {
return searchArray.map(curr => ({[curr]: targetArray.filter(check => curr === check).length}));
}

magnusfohlstrom
Автор

Have to practice this two time arrays comparing.... 😃

MuhammadAdnan.
Автор

returns from one line...
function compareArray( searchArray, targetArray ) {
return searchArray.reduce((obj, curr) => [...obj, {[curr]: targetArray.filter(check => curr === check).length}], []);
}

magnusfohlstrom
Автор

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).

lidwinguillermogascagarcia
Автор

i was under the impression that those higher order functions map, filter, reduce etc.. all had built in loops to them.

Charlie_
Автор

Nice tutorial, thanks. IMO, it would be more helpful and relevant if you were to use arrow functions.

ScottL
Автор

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

hamidrezaakbarnezhad
Автор

wordList.filter( conditional checking for === with word in wordCheck).length

TheSwagStatus
Автор

Wouldn't JSON.parse(JSON.stringify) work for this? I use it for comparing an array of objects

chrismiller
Автор

Hahahah. No loop but map and reduce :)))). Dislike.

MiroslavPopov
Автор

.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;

objArray[controller] = {[searchArray[controller]]: tot};
++controller !== searchArrayLen && fill(controller);
})();
return objArray;
};

magnusfohlstrom