JavaScript Problem: Comparing Two Arrays without Loops

JavaScript Problem: Comparing Two Arrays without Loops

All Things JavaScript, LLC

3 года назад

16,074 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@hammadrana364
@hammadrana364 - 14.02.2023 23:53

map also a loopp

Ответить
@lidwinguillermogascagarcia439
@lidwinguillermogascagarcia439 - 21.12.2022 05:06

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

Ответить
@sajeersayed2008
@sajeersayed2008 - 08.11.2022 07:57

Perfect

Ответить
@MiroslavPopov
@MiroslavPopov - 19.08.2022 10:02

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

Ответить
@amatiasq
@amatiasq - 28.11.2020 15:18

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

Ответить
@ScottL888
@ScottL888 - 26.11.2020 20:58

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

Ответить
@hamidrezaakbarnezhad2018
@hamidrezaakbarnezhad2018 - 13.11.2020 12:33

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

Ответить
@user-zb5jp4ti1d
@user-zb5jp4ti1d - 12.11.2020 21:24

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

Ответить
@TheSwagStatus
@TheSwagStatus - 12.11.2020 18:31

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

Ответить
@Charlie_904
@Charlie_904 - 12.11.2020 09:35

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

Ответить
@chrismiller3650
@chrismiller3650 - 12.11.2020 05:04

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

Ответить
@magnusfohlstrom
@magnusfohlstrom - 12.11.2020 04:57

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

Ответить
@magnusfohlstrom
@magnusfohlstrom - 12.11.2020 03:58

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

Ответить
@magnusfohlstrom
@magnusfohlstrom - 12.11.2020 03:30

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

Ответить
@tirmey
@tirmey - 12.11.2020 02:45

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

Ответить
@MuhammadAdnan2.0
@MuhammadAdnan2.0 - 12.11.2020 01:29

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

Ответить
@cpu1001
@cpu1001 - 12.11.2020 01:22

Awesome video, thanks for sharing!

Ответить
@zhasan66
@zhasan66 - 12.11.2020 01:21

Very helpful video tutorial.

Ответить