0
I have a question in Javascript.
When I splice an array, it removes what?
For example, I have an array with several objects inside, as in the example below:
lixo: [
{"nome": "garrafa pet", "tipo": "reciclável", "cod": 12, "armazenavel": false},
{"nome": "lata de ref.", "tipo": "não reciclável", "cod": 22, "armazenavel": true},
{"nome": "lapis de cor", "tipo": "não reciclável", "cod": 107, "armazenavel": false},
],
If I do it here:
for(let i = 0; i <= lixo.lenght; i++){
if(lixo[i].nome == "lata de ref."{
let coleta = lixo.splice(i, 1)
}
}
In this case above, after I circled the for and entered the if, the Collection variable will receive what? by chance it receives another array with the object of the ref. can inside? or Collection will only be an object of the ref can.?
If it is a collecting array that is receiving, how do I make it store only the object of the ref can.? Like this, I don’t want it to receive an array of a single object from the ref can.. In case, I want that collection receives only the object extracted from the array.
Does anyone know how to collect receiving only an object and not an array with an object inside?
splice
– Sorack
Related: Array.splice does not work as expected
– Sorack
But if your goal is to get the object in position because you use
splice
? Why don’t you justlet coleta = lixo[i];
?– Isac