Комментарии:
map also a loopp
Ответить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).
ОтветитьPerfect
ОтветитьHahahah. No loop but map and reduce :)))). Dislike.
Ответить"Comparing Two Arrays without Loops", then proceeds to nest a `.reduce()` inside a `.map()`...
ОтветитьNice tutorial, thanks. IMO, it would be more helpful and relevant if you were to use arrow functions.
Ответить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
ОтветитьThanks Steve for going to such great lengths to answer the query... you are a legend sir :)
ОтветитьwordList.filter( conditional checking for === with word in wordCheck).length
Ответитьi was under the impression that those higher order functions map, filter, reduce etc.. all had built in loops to them.
ОтветитьWouldn't JSON.parse(JSON.stringify) work for this? I use it for comparing an array of objects
ОтветитьWithout reduce...
function compareArray( searchArray, targetArray ) {
return searchArray.map(curr => ({[curr]: targetArray.filter(check => curr === check).length}));
}
returns from one line...
function compareArray( searchArray, targetArray ) {
return searchArray.reduce((obj,curr) => [...obj, {[curr]: targetArray.filter(check => curr === check).length}], []);
}
.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;
};
Mapping over an array is kind of intrinsically lopping over it, no? nice solution, anyway!
ОтветитьHave to practice this two time arrays comparing.... 😃
ОтветитьAwesome video, thanks for sharing!
ОтветитьVery helpful video tutorial.
Ответить