Remove Object out an Array
up vote
0
down vote
favorite
I'm working on an application where I can add groups to a user. You can choose a group from a list and that group will show above of it. (See this photo)
I want now that you only can see the groups in the dropdown where you don't belong to.
constructor(props) {
super(props)
this.state = {
groups: ,
selectedGroups: ,
group: '',
isLoading: true,
show: false
};
//bind
this.getGroups = this.getGroups.bind(this);
this.addGroup = this.addGroup.bind(this);
}
//this function will be executed when clicking on button
addGroup(e) {
e.preventDefault();
this.setState({
selectedGroups: [...this.state.selectedGroups, this.state.group], //hier worden de groepen van die gebruiker in geplaatst
groups: //hier moet iets gebeuren dat this.state.group wordt verwijdert uit de lijst
})}
My propery groups look like this:
[{"id":1,"company_id":1,"name":"Support","created_at":null,"updated_at":null},
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
My property, group, is the selected group when clicking on the button.
Example: if I select Administrator and click on the button, the property group will have "Administrator" as value. Administrator will then be add to the Array selectedGroups. Now I want that the object with the name "Administrator" removes from the groups array.
The property groups have to look like this then:
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
How can I delete a object from an Array of objects?
arrays reactjs object
add a comment |
up vote
0
down vote
favorite
I'm working on an application where I can add groups to a user. You can choose a group from a list and that group will show above of it. (See this photo)
I want now that you only can see the groups in the dropdown where you don't belong to.
constructor(props) {
super(props)
this.state = {
groups: ,
selectedGroups: ,
group: '',
isLoading: true,
show: false
};
//bind
this.getGroups = this.getGroups.bind(this);
this.addGroup = this.addGroup.bind(this);
}
//this function will be executed when clicking on button
addGroup(e) {
e.preventDefault();
this.setState({
selectedGroups: [...this.state.selectedGroups, this.state.group], //hier worden de groepen van die gebruiker in geplaatst
groups: //hier moet iets gebeuren dat this.state.group wordt verwijdert uit de lijst
})}
My propery groups look like this:
[{"id":1,"company_id":1,"name":"Support","created_at":null,"updated_at":null},
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
My property, group, is the selected group when clicking on the button.
Example: if I select Administrator and click on the button, the property group will have "Administrator" as value. Administrator will then be add to the Array selectedGroups. Now I want that the object with the name "Administrator" removes from the groups array.
The property groups have to look like this then:
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
How can I delete a object from an Array of objects?
arrays reactjs object
2
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Alexander Staroselsky
Nov 7 at 17:34
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on an application where I can add groups to a user. You can choose a group from a list and that group will show above of it. (See this photo)
I want now that you only can see the groups in the dropdown where you don't belong to.
constructor(props) {
super(props)
this.state = {
groups: ,
selectedGroups: ,
group: '',
isLoading: true,
show: false
};
//bind
this.getGroups = this.getGroups.bind(this);
this.addGroup = this.addGroup.bind(this);
}
//this function will be executed when clicking on button
addGroup(e) {
e.preventDefault();
this.setState({
selectedGroups: [...this.state.selectedGroups, this.state.group], //hier worden de groepen van die gebruiker in geplaatst
groups: //hier moet iets gebeuren dat this.state.group wordt verwijdert uit de lijst
})}
My propery groups look like this:
[{"id":1,"company_id":1,"name":"Support","created_at":null,"updated_at":null},
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
My property, group, is the selected group when clicking on the button.
Example: if I select Administrator and click on the button, the property group will have "Administrator" as value. Administrator will then be add to the Array selectedGroups. Now I want that the object with the name "Administrator" removes from the groups array.
The property groups have to look like this then:
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
How can I delete a object from an Array of objects?
arrays reactjs object
I'm working on an application where I can add groups to a user. You can choose a group from a list and that group will show above of it. (See this photo)
I want now that you only can see the groups in the dropdown where you don't belong to.
constructor(props) {
super(props)
this.state = {
groups: ,
selectedGroups: ,
group: '',
isLoading: true,
show: false
};
//bind
this.getGroups = this.getGroups.bind(this);
this.addGroup = this.addGroup.bind(this);
}
//this function will be executed when clicking on button
addGroup(e) {
e.preventDefault();
this.setState({
selectedGroups: [...this.state.selectedGroups, this.state.group], //hier worden de groepen van die gebruiker in geplaatst
groups: //hier moet iets gebeuren dat this.state.group wordt verwijdert uit de lijst
})}
My propery groups look like this:
[{"id":1,"company_id":1,"name":"Support","created_at":null,"updated_at":null},
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
My property, group, is the selected group when clicking on the button.
Example: if I select Administrator and click on the button, the property group will have "Administrator" as value. Administrator will then be add to the Array selectedGroups. Now I want that the object with the name "Administrator" removes from the groups array.
The property groups have to look like this then:
{"id":2,"company_id":1,"name":"Administrator","created_at":null,"updated_at":null},
{"id":3,"company_id":1,"name":"Redacteur","created_at":null,"updated_at":null}]
How can I delete a object from an Array of objects?
arrays reactjs object
arrays reactjs object
asked Nov 7 at 17:28
willem
104
104
2
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Alexander Staroselsky
Nov 7 at 17:34
add a comment |
2
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Alexander Staroselsky
Nov 7 at 17:34
2
2
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Alexander Staroselsky
Nov 7 at 17:34
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Alexander Staroselsky
Nov 7 at 17:34
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
To my mind, in your case you need to find an index of the element in the array which you wanna delete by using method below:
const index = array.findIndex(value => value.id === id);
and then delete this index:
const result = array.splice(index, 1);
Where array
it is your array where you wanna delete the object, id
this an ID of an object which should be deleted. You can also check if a findIndex
has founded something before doing splice
and if nothing founded findIndex
will return -1
.
findIndex doc ref
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
To my mind, in your case you need to find an index of the element in the array which you wanna delete by using method below:
const index = array.findIndex(value => value.id === id);
and then delete this index:
const result = array.splice(index, 1);
Where array
it is your array where you wanna delete the object, id
this an ID of an object which should be deleted. You can also check if a findIndex
has founded something before doing splice
and if nothing founded findIndex
will return -1
.
findIndex doc ref
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
add a comment |
up vote
2
down vote
accepted
To my mind, in your case you need to find an index of the element in the array which you wanna delete by using method below:
const index = array.findIndex(value => value.id === id);
and then delete this index:
const result = array.splice(index, 1);
Where array
it is your array where you wanna delete the object, id
this an ID of an object which should be deleted. You can also check if a findIndex
has founded something before doing splice
and if nothing founded findIndex
will return -1
.
findIndex doc ref
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
To my mind, in your case you need to find an index of the element in the array which you wanna delete by using method below:
const index = array.findIndex(value => value.id === id);
and then delete this index:
const result = array.splice(index, 1);
Where array
it is your array where you wanna delete the object, id
this an ID of an object which should be deleted. You can also check if a findIndex
has founded something before doing splice
and if nothing founded findIndex
will return -1
.
findIndex doc ref
To my mind, in your case you need to find an index of the element in the array which you wanna delete by using method below:
const index = array.findIndex(value => value.id === id);
and then delete this index:
const result = array.splice(index, 1);
Where array
it is your array where you wanna delete the object, id
this an ID of an object which should be deleted. You can also check if a findIndex
has founded something before doing splice
and if nothing founded findIndex
will return -1
.
findIndex doc ref
edited Nov 7 at 18:01
answered Nov 7 at 17:51
Eugene Dzhevadov
1064
1064
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
add a comment |
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
Thanks a lot. This is absolutely working!
– willem
Nov 7 at 18:05
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
I am glad that helped, you're welcome
– Eugene Dzhevadov
Nov 7 at 18:16
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194712%2fremove-object-out-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Alexander Staroselsky
Nov 7 at 17:34