0
Today I have the following constant:
const alunos = [{
id: 1,
nome: 'Julia',
curso: 'Enfermagem',
}, {
id: 2,
nome: 'Isabela',
curso: 'Veterinária'
}]
but I would need to just erase the id data, which is set in the state and update it.
0
Today I have the following constant:
const alunos = [{
id: 1,
nome: 'Julia',
curso: 'Enfermagem',
}, {
id: 2,
nome: 'Isabela',
curso: 'Veterinária'
}]
but I would need to just erase the id data, which is set in the state and update it.
1
Scan the array and search for Ids, then remove using delete
const alunos = [{
id: 1,
nome: 'Julia',
curso: 'Enfermagem',
}, {
id: 2,
nome: 'Isabela',
curso: 'Veterinária'
}];
for (var i =0; i < alunos.length; i++){
delete alunos[i].id;
}
console.log(alunos);
1
Friend, your question is a little difficult to understand, maybe even because you still do not understand very well React.
You said you want to delete an item from 'React Status'. This means to me that you want to delete from this.state
, correct? If it is, for what reason showed an array constant with 2 objects and not the this.state
?
Moreover, in the this.state
you never delete anything, nor update anything directly (this.state.attribute = 'some value'). You always let React update to you through this.setState({})
. For example, to delete an attribute just set it to Undefined: this.setState({ foo: undefined })
Improve your doubt statement to help you more.
0
As our friend Gleidson Henrique commented his question is not clear but I will try to help as I understand.
You have a constant within the Right State ? Take as an example this my button state
{this.state.Buttons.map((button, index) => delete button[index] )}
I hope I’ve helped
0
Just use the method filter
to keep only properties that have an ID in the list different of the one you wish to exclude.
For example:
const state = [{
id: 1,
nome: 'Julia',
curso: 'Enfermagem',
}, {
id: 2,
nome: 'Isabela',
curso: 'Veterinária'
}]
// ID que desejamos excluir:
const id = 1
const newState = state.filter((data) => data.id !== id)
console.log(newState)
To learn more about this method, read the documentation Array.prototype.filter
on MDN.
0
Do you intend to delete the item that is stored in the state? Could:
const alunos = [{
id: 1,
nome: 'Julia',
curso: 'Enfermagem',
}, {
id: 2,
nome: 'Isabela',
curso: 'Veterinária'
}]
const index = alunos.findIndex( i => i.id == this.state.alunos.id);
alunos.splice(index, 1);
// Ou se for somente o id
delete alunos[index].id;
Browser other questions tagged javascript react jsx
You are not signed in. Login or sign up in order to post.
Thales, I think you need to be a little clearer. Do you want to update or delete? What have you tried so far? Did you give any error? Because what you tried is not working?
– fernandosavio
By adding the "Douglas Teles" response you can make use of the
map
in place ofFOR
. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map– fullstack-overkill