Reputation: 39
I have the following object:
obj = {"foo":"bar", "foo2":"bar2", "foo3":"bar3"};
I've been trying with a few iterations but haven't been able to catch the values (bar, bar2, bar3), only the keys.
What I need to do is to clear those values, so the keys happen to be all empty.
I've tried the following:
$.each( obj, function( key, value ) {
delete value;
// or also: value = '';
});
And also stuff like:
$(obj).each(function (key,value) -- //same content as last one.
Object.values(obj).forEach(function (key) {
var value = obj[key];
});
Upvotes: 0
Views: 347
Reputation: 18958
You can just do this by looping:
var test = {"a":"abc", "b":"ref", "c": "def"}
console.log("Before:");
console.log(test);
for (var key in test) {
test[key] = "";
}
console.log("After:");
console.log(test);
Upvotes: 1
Reputation: 18429
Iterate through that object and set the value to null
/undefined
or whatever you want as empty value.
var obj = {"foo":"bar", "foo2":"bar2", "foo3":"bar3"};
Object.keys(obj).forEach(k => { obj[k] = null; })
console.log(obj);
Or if you want to remove the key, use delete
:
var obj = {"foo":"bar", "foo2":"bar2", "foo3":"bar3"};
Object.keys(obj).forEach(k => { delete obj[k]; })
console.log(obj);
Upvotes: 1