Created
May 20, 2016 11:59
-
-
Save evaldasg/37b3605b954f26c9110dbc7f956641e7 to your computer and use it in GitHub Desktop.
Immutable state; remove property based on key which is passed;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var state = { | |
0: "zero", | |
1: "one", | |
2: "two", | |
x: "hello" | |
}; | |
function removeByKey (myObj, deleteKey) { | |
return Object.keys(myObj) | |
.filter(key => key !== deleteKey) | |
.reduce((result, current) => { | |
result[current] = myObj[current]; | |
return result; | |
}, {}); | |
} | |
console.log("Current State:"); | |
console.log(state); | |
var stateMinusFirst = removeByKey(state, "0"); | |
console.log("State minus zero key:"); | |
console.log(stateMinusFirst); | |
var stateMinusX = removeByKey(state, "x"); | |
console.log("State minus x key:"); | |
console.log(stateMinusX); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment