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?










share|improve this question


















  • 2




    Possible duplicate of How do I remove a particular element from an array in JavaScript?
    – Alexander Staroselsky
    Nov 7 at 17:34















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?










share|improve this question


















  • 2




    Possible duplicate of How do I remove a particular element from an array in JavaScript?
    – Alexander Staroselsky
    Nov 7 at 17:34













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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






share|improve this answer























  • 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















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






share|improve this answer























  • 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













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini