How to remove an element from a key value array?
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
add a comment |
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
Possible duplicate of How do I remove objects from a javascript associative array?
– Erik Terwan
Jan 2 at 15:02
add a comment |
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
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
javascript reactjs react-redux
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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 });
}
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
add a comment |
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 });
}
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
add a comment |
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
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
add a comment |
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
});
}
});
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%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
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 });
}
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
add a comment |
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 });
}
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
add a comment |
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 });
}
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 });
}
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
add a comment |
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
add a comment |
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 });
}
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
add a comment |
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 });
}
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
add a comment |
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 });
}
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 });
}
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
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%2f54008511%2fhow-to-remove-an-element-from-a-key-value-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
Possible duplicate of How do I remove objects from a javascript associative array?
– Erik Terwan
Jan 2 at 15:02