-1
I have a array
I need to sweep with the for
of
javascript and need to check if my current element has the id == 2
If there is, I need to clone it and put it right after the current element.
Example:
let data = [{id: 1}, {id: 2}, {id: 3}]
for (const dat of data) {
if (dat.id == 2) {
// quero inserir entre id 2 e id 3 um clone do meu elemento atual
// que no caso é o 2
}
}
// Resultado esperado
[{id: 1}, {id: 2}, {id: 2}, {id: 3}]
Is it possible? or is it better to create an auxiliary array and write to it?