How to remove an element from a key value array?












1















I want to remove a specific element from an array, I am getting the key of the element from the input.
I want to be able to remove the element only by knowing the key.



This is the array:



state ={
splitAmount : [{
"SplitAmount0": this.props.data.amount1
}, {
"SplitAmount1": this.props.data.amount2
}, {
"SplitAmount2": this.props.data.amount3
}]
}


Remove function:



  removeSplitAmount(e) {
console.log("remove",e.target.name)
let array = [...this.state.splitAmount];
let index = this.state.splitAmount.IndexOf(p => p == e.target.name )
if (index !== -1) {
array.splice(index, 1);
this.setState({splitAmount: array});
}
}









share|improve this question

























  • Possible duplicate of How do I remove objects from a javascript associative array?

    – Erik Terwan
    Jan 2 at 15:02
















1















I want to remove a specific element from an array, I am getting the key of the element from the input.
I want to be able to remove the element only by knowing the key.



This is the array:



state ={
splitAmount : [{
"SplitAmount0": this.props.data.amount1
}, {
"SplitAmount1": this.props.data.amount2
}, {
"SplitAmount2": this.props.data.amount3
}]
}


Remove function:



  removeSplitAmount(e) {
console.log("remove",e.target.name)
let array = [...this.state.splitAmount];
let index = this.state.splitAmount.IndexOf(p => p == e.target.name )
if (index !== -1) {
array.splice(index, 1);
this.setState({splitAmount: array});
}
}









share|improve this question

























  • Possible duplicate of How do I remove objects from a javascript associative array?

    – Erik Terwan
    Jan 2 at 15:02














1












1








1








I want to remove a specific element from an array, I am getting the key of the element from the input.
I want to be able to remove the element only by knowing the key.



This is the array:



state ={
splitAmount : [{
"SplitAmount0": this.props.data.amount1
}, {
"SplitAmount1": this.props.data.amount2
}, {
"SplitAmount2": this.props.data.amount3
}]
}


Remove function:



  removeSplitAmount(e) {
console.log("remove",e.target.name)
let array = [...this.state.splitAmount];
let index = this.state.splitAmount.IndexOf(p => p == e.target.name )
if (index !== -1) {
array.splice(index, 1);
this.setState({splitAmount: array});
}
}









share|improve this question
















I want to remove a specific element from an array, I am getting the key of the element from the input.
I want to be able to remove the element only by knowing the key.



This is the array:



state ={
splitAmount : [{
"SplitAmount0": this.props.data.amount1
}, {
"SplitAmount1": this.props.data.amount2
}, {
"SplitAmount2": this.props.data.amount3
}]
}


Remove function:



  removeSplitAmount(e) {
console.log("remove",e.target.name)
let array = [...this.state.splitAmount];
let index = this.state.splitAmount.IndexOf(p => p == e.target.name )
if (index !== -1) {
array.splice(index, 1);
this.setState({splitAmount: array});
}
}






javascript reactjs react-redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 15:04









cbass

2,17221933




2,17221933










asked Jan 2 at 14:57









MalekMalek

226




226













  • Possible duplicate of How do I remove objects from a javascript associative array?

    – Erik Terwan
    Jan 2 at 15:02



















  • Possible duplicate of How do I remove objects from a javascript associative array?

    – Erik Terwan
    Jan 2 at 15:02

















Possible duplicate of How do I remove objects from a javascript associative array?

– Erik Terwan
Jan 2 at 15:02





Possible duplicate of How do I remove objects from a javascript associative array?

– Erik Terwan
Jan 2 at 15:02












3 Answers
3






active

oldest

votes


















2














You can use the .filter method on the array combined with the Object.keys to clean the function up a lot:



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(p => !Object.keys(p).includes(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer


























  • working perfectly Thanks !!!!

    – Malek
    Jan 2 at 15:14











  • Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

    – Frank
    Jan 9 at 21:35





















1














You can use hasOwnProperty to filter objects you need.



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(x => !x.hasOwnProperty(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer


























  • I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

    – Frank
    Jan 9 at 21:40



















1














As Dmitry said you can't do a indexOf on an array of objects... i felt bad i didn't realize that.
Would be useful on this case:
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];



beasts.indexOf('bison')



On your case you are better to go with the .filter method as said in above answers because you are searching for and object with a specific property






share|improve this answer


























  • indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

    – Dmitry
    Jan 2 at 15:12











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',
autoActivateHeartbeat: false,
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%2f54008511%2fhow-to-remove-an-element-from-a-key-value-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use the .filter method on the array combined with the Object.keys to clean the function up a lot:



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(p => !Object.keys(p).includes(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer


























  • working perfectly Thanks !!!!

    – Malek
    Jan 2 at 15:14











  • Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

    – Frank
    Jan 9 at 21:35


















2














You can use the .filter method on the array combined with the Object.keys to clean the function up a lot:



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(p => !Object.keys(p).includes(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer


























  • working perfectly Thanks !!!!

    – Malek
    Jan 2 at 15:14











  • Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

    – Frank
    Jan 9 at 21:35
















2












2








2







You can use the .filter method on the array combined with the Object.keys to clean the function up a lot:



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(p => !Object.keys(p).includes(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer















You can use the .filter method on the array combined with the Object.keys to clean the function up a lot:



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(p => !Object.keys(p).includes(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 15:04









Christiaan

76136




76136










answered Jan 2 at 15:03









dandan

826620




826620













  • working perfectly Thanks !!!!

    – Malek
    Jan 2 at 15:14











  • Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

    – Frank
    Jan 9 at 21:35





















  • working perfectly Thanks !!!!

    – Malek
    Jan 2 at 15:14











  • Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

    – Frank
    Jan 9 at 21:35



















working perfectly Thanks !!!!

– Malek
Jan 2 at 15:14





working perfectly Thanks !!!!

– Malek
Jan 2 at 15:14













Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

– Frank
Jan 9 at 21:35







Make sure to include a polyfill for the “includes” function if you need to support older browsers like IE11

– Frank
Jan 9 at 21:35















1














You can use hasOwnProperty to filter objects you need.



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(x => !x.hasOwnProperty(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer


























  • I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

    – Frank
    Jan 9 at 21:40
















1














You can use hasOwnProperty to filter objects you need.



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(x => !x.hasOwnProperty(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer


























  • I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

    – Frank
    Jan 9 at 21:40














1












1








1







You can use hasOwnProperty to filter objects you need.



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(x => !x.hasOwnProperty(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}





share|improve this answer















You can use hasOwnProperty to filter objects you need.



removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(x => !x.hasOwnProperty(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 15:15

























answered Jan 2 at 15:09









DmitryDmitry

3,904102730




3,904102730













  • I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

    – Frank
    Jan 9 at 21:40



















  • I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

    – Frank
    Jan 9 at 21:40

















I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

– Frank
Jan 9 at 21:40





I think this is the best answer even though the “newSplitAmount” variable is not even necessary and could be removed if you want even less code

– Frank
Jan 9 at 21:40











1














As Dmitry said you can't do a indexOf on an array of objects... i felt bad i didn't realize that.
Would be useful on this case:
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];



beasts.indexOf('bison')



On your case you are better to go with the .filter method as said in above answers because you are searching for and object with a specific property






share|improve this answer


























  • indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

    – Dmitry
    Jan 2 at 15:12
















1














As Dmitry said you can't do a indexOf on an array of objects... i felt bad i didn't realize that.
Would be useful on this case:
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];



beasts.indexOf('bison')



On your case you are better to go with the .filter method as said in above answers because you are searching for and object with a specific property






share|improve this answer


























  • indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

    – Dmitry
    Jan 2 at 15:12














1












1








1







As Dmitry said you can't do a indexOf on an array of objects... i felt bad i didn't realize that.
Would be useful on this case:
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];



beasts.indexOf('bison')



On your case you are better to go with the .filter method as said in above answers because you are searching for and object with a specific property






share|improve this answer















As Dmitry said you can't do a indexOf on an array of objects... i felt bad i didn't realize that.
Would be useful on this case:
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];



beasts.indexOf('bison')



On your case you are better to go with the .filter method as said in above answers because you are searching for and object with a specific property







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 15:31

























answered Jan 2 at 15:02









Hugo BarragonHugo Barragon

645




645













  • indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

    – Dmitry
    Jan 2 at 15:12



















  • indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

    – Dmitry
    Jan 2 at 15:12

















indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

– Dmitry
Jan 2 at 15:12





indexOf does not take lmbda as parameter. Unless you want to find an index of a lambda in an array of lambdas.

– Dmitry
Jan 2 at 15:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008511%2fhow-to-remove-an-element-from-a-key-value-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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory