How to make funkyonalnom style. The function returns true if limit is found. Searches for duplicates that repeat 2+ times

export default function(array, currentdata, limit = 2) { const { ac, sub} = currentData; let counter = 0; for (let j = 0; j < array.length; j++) { if (counter >= limit) { return true; } if (array[j].type1 === ac && array[j].type2 === sub) { counter++; } } return false; } 
  • return array.some(function(el){return (this.limit-=array[j].type1 === ac && array[j].type2 === sub)<1},{limit}) - Grundy
  • and where does array [j] come from? maybe el? - DFGD
  • Yes, just copied the string you had, el was supposed to be there - Grundy

1 answer 1

Parameter destructurization can be done in the declaration itself:

const { ac, sub} = currentData; -> function(array, { ac, sub}, limit = 2) {

To check the satisfaction of the elements condition, you can use the function .some

When the condition is met, you can change the limit parameter itself, and stop the search when it reaches the value 0 .

As a result, the function code may look like this:

 export default function(array, {ac, sub}, limit = 2) { return array.some(el=>(limit-=el.type1 === ac && el.type2 === sub)<1); }