The delete operator allows you to remove a property from an object.
The following examples all do the same thing.
// Example 1
var key = "Cow";
delete thisIsObject[key];
// Example 2
delete thisIsObject["Cow"];
// Example 3
delete thisIsObject.Cow;
let animals = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
delete animals.Cow;
delete animals['Dog'];
console.log(animals);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you're interested, read Understanding Delete for an in-depth explanation.
Answer from jessegavin on Stack OverflowThe delete operator allows you to remove a property from an object.
The following examples all do the same thing.
// Example 1
var key = "Cow";
delete thisIsObject[key];
// Example 2
delete thisIsObject["Cow"];
// Example 3
delete thisIsObject.Cow;
let animals = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
delete animals.Cow;
delete animals['Dog'];
console.log(animals);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you're interested, read Understanding Delete for an in-depth explanation.
If you are using Underscore.js or Lodash, there is a function 'omit' that will do it.
http://underscorejs.org/#omit
var thisIsObject= {
'Cow' : 'Moo',
'Cat' : 'Meow',
'Dog' : 'Bark'
};
_.omit(thisIsObject,'Cow'); //It will return a new object
=> {'Cat' : 'Meow', 'Dog' : 'Bark'} //result
If you want to modify the current object, assign the returning object to the current object.
thisIsObject = _.omit(thisIsObject,'Cow');
With pure JavaScript, use:
delete thisIsObject['Cow'];
Another option with pure JavaScript.
thisIsObject = Object.keys(thisIsObject).filter(key =>
key !== 'cow').reduce((obj, key) =>
{
obj[key] = thisIsObject[key];
return obj;
}, {}
);
javascript - Remove item from object by key - Stack Overflow
How to delete a javascript object item by value? - Stack Overflow
javascript - Remove element by key from all the objects inside an object - Stack Overflow
How do I remove a property from a JavaScript object? - Stack Overflow
You should use immutable operations when using Redux. You should not change your state directly.
For that, you can use destructuring to exclude your todo from the todos, if your todos are in an object:
const { todos } = this.state;
const { [id]: _, ...newTodos } = todos;
this.setState({
todos: newTodos
});
If the todos are in a list and since you cannot destructure an item by index from an array, use the slice method, which doesn't modify the array, but returns a modified copy:
const { todos } = this.state;
this.setState({
todos: [...todos.slice(0, id), ...todos.slice(id + 1)];
});
You have to firstly copy your state to an array so you have a clone of it.
Then you remove the unwanted id from your new array.
var newTodoArray = this.state;
newTodoArray.remove(id);
this.setState({
todos: newTodoArray,
});
Something like the above.
Have you tried something like this?
function deleteByValue(val) {
for(var f in fruits) {
if(fruits[f] == val) {
delete fruits[f];
}
}
}
And as per Rocket's comment, you might want to check hasOwnProperty to make sure you aren't deleting members of the object's prototype:
function deleteByValue(val) {
for(var f in fruits) {
if(fruits.hasOwnProperty(f) && fruits[f] == val) {
delete fruits[f];
}
}
}
var key = null;
for (var k in fruits){
if (fruits[k] === 'apple'){
key = k;
break;
}
}
if (key != null)
delete fruits[key];
Iterate over the object finding the corresponding key, then remove it (if found).
You can use something like this:
const data = {
"/test2": {
"path": "/test",
"items": [{
"path": "/test",
"method": "GET",
}, {
"path": "/test",
"method": "PUT",
}]
},
"/test": {
"path": "/test2",
"items": [{
"path": "/test2",
"method": "GET",
}]
}
}
Object.keys(data).forEach(k => {
data[k].items.forEach(item => {
delete item['path']
})
})
console.log(data)
jsfiddle
You can use Object.entries to convert the object into array. Use reduce to loop thru the array. Use map to loop thru items and only return the method
const test = {"/test2":{"path":"/test","items":[{"path":"/test","method":"GET"},{"path":"/test","method":"PUT"}]},"/test":{"path":"/test2","items":[{"path":"/test2","method":"GET"}]}};
const result = Object.entries(test).reduce((c, [k, {path,items}]) => {
c[k] = {path};
c[k].items = items.map(({method}) => ({method}));
return c;
}, {});
console.log(result);
To remove a property from an object (mutating the object), you can do it by using the delete keyword, like this:
delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];
Demo
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject.regex;
console.log(myObject);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
For anyone interested in reading more about it, Stack Overflow user kangax has written an incredibly in-depth blog post about the delete statement on their blog, Understanding delete. It is highly recommended.
If you'd like a new object with all the keys of the original except some, you could use destructuring.
Demo
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
// assign the key regex to the variable _ indicating it will be unused
const { regex: _, ...newObj } = myObject;
console.log(newObj); // has no 'regex' key
console.log(myObject); // remains unchanged
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Objects in JavaScript can be thought of as maps between keys and values. The delete operator is used to remove these keys, more commonly known as object properties, one at a time.
var obj = {
myProperty: 1
}
console.log(obj.hasOwnProperty('myProperty')) // true
delete obj.myProperty
console.log(obj.hasOwnProperty('myProperty')) // false
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object. Note that if the value of a deleted property was a reference type (an object), and another part of your program still holds a reference to that object, then that object will, of course, not be garbage collected until all references to it have disappeared.
delete will only work on properties whose descriptor marks them as configurable.