R
R
R02021-06-21 15:32:06
JavaScript
R0, 2021-06-21 15:32:06

How to replace for with .reduce?

I'm trying to reduce the number of lines and even got stuck. There is such code:
for (let i = 0; i < rClon.length; i++) {
if (rMax === rClon[i]) {
rClon.splice(i, 1);
break;
}
} I
tried to remake it into this and at the moment with the break it gives an error
rClon.reduce((rNone, i1) => rMax === i ? rClon.splice(i1, 1); break;);
I don’t understand how to write it down,

the loop should find the first element of rMax in rClon, delete it and break
, maybe it’s not necessary to use rudus here?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-06-21
@R0_none

Methods .forEach, .map, .reduice, etc. not interrupted.
.indexOf is enough here

const idx = rClon.indexOf(rMax);
if (idx !== -1) {
  rClon.splice(idx, 1);
}

A
abberati, 2021-06-21
@abberati

const result = rClon.filter(x => rMax !== x));
But that's not really it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question