I want to replace my for loop by higher order functions in my problem.
I need to create a function that takes an array as parameter and return an array with the values of the input array that match x, let's say 10.
For exemple
const matchesValues = ( array ) => {
//MyFunc
}
console.log(matchesValues([2,8,5,5,6])) // [[2,8], [5,5]]
Here is what I have done for the moment:
const matchesValues = (array) => { if (array.length > 1) { const matches = [] for (let i = array.length - 1; i >= 0; i--) { if (i - 1 >= 0) { if (array[i] + array[i - 1] == 10) { matches.push([array[i - 1], array[i]]) } } } return matches } } console.log(matchesValues([2,8,5,5,5,6])) // expected: [2,8,5,5], recieved: [[5,5], [5,5], [2,8]]
Note that the order would stay the same, but this I can handle.
By which higher order function(s) would you replace my forloop?
Thanks a lot for your time.
Use reduce
const matchesValues = ( array ) => { return array.reduce((previousValue, currentValue, currentIndex) => { if (currentIndex === 0 || (array[currentIndex - 1] + currentValue) === 10) { previousValue.push(currentValue); } return previousValue; }, []); }; console.log(matchesValues([2,8,5,5,5,6]));
there is a longer solution here but with the same return result format
[[2,8], [5,5]]
what you requested. Also it handles the case when there is only 1 value that matches up the sum
, eg: 10
.
const matchValues = (array, valueToMatch) => {
const matchingValues = [];
if(!array.length) {
return matchingValues;
}
array.forEach((actualValue, index) => {
if((array.length-1) === index) {
if(actualValue === valueToMatch) {
matchingValues.push([actualValue]);
}
}
const clonedArray = array.filter((_, clonedValueIndex) => clonedValueIndex !== index);
clonedArray.forEach((value) => {
if((value + actualValue) === valueToMatch) {
let alreadyMatched = false;
if(matchingValues.length) {
matchingValues.forEach((matchingValue) => {
if(matchingValue.includes(value) && matchingValue.includes(actualValue) && (value + actualValue === valueToMatch)) {
alreadyMatched = true;
}
})
}
if(!alreadyMatched) {
matchingValues.push([actualValue, value]);
}
}
})
});
return matchingValues;
}
const returnedMatchingvalues = matchValues([2,8,5,5,5,6,10], 10);
console.log(returnedMatchingvalues); // [ [ 2, 8 ], [ 5, 5 ], [ 10 ] ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.