Skip to content

Instantly share code, notes, and snippets.

@evaldasg
Created May 20, 2016 11:59
Show Gist options
  • Save evaldasg/37b3605b954f26c9110dbc7f956641e7 to your computer and use it in GitHub Desktop.
Save evaldasg/37b3605b954f26c9110dbc7f956641e7 to your computer and use it in GitHub Desktop.
Immutable state; remove property based on key which is passed;
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