var json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.
You can use splice to remove elements from an array.
Answer from dteoh on Stack Overflowvar json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.
You can use splice to remove elements from an array.
Do NOT have trailing commas in your OBJECT (JSON is a string notation)
UPDATE: you need to use array.splice and not delete if you want to remove items from the array in the object. Alternatively filter the array for undefined after removing
var data = {
"result": [{
"FirstName": "Test1",
"LastName": "User"
}, {
"FirstName": "user",
"LastName": "user"
}]
}
console.log(data.result);
console.log("------------ deleting -------------");
delete data.result[1];
console.log(data.result); // note the "undefined" in the array.
data = {
"result": [{
"FirstName": "Test1",
"LastName": "User"
}, {
"FirstName": "user",
"LastName": "user"
}]
}
console.log(data.result);
console.log("------------ slicing -------------");
var deletedItem = data.result.splice(1,1);
console.log(data.result); // here no problem with undefined.
To iterate through the keys of an object, use a for .. in loop:
for (var key in json_obj) {
if (json_obj.hasOwnProperty(key)) {
// do something with `key'
}
}
To test all elements for empty children, you can use a recursive approach: iterate through all elements and recursively test their children too.
Removing a property of an object can be done by using the delete keyword:
var someObj = {
"one": 123,
"two": 345
};
var key = "one";
delete someObj[key];
console.log(someObj); // prints { "two": 345 }
Documentation:
- https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete
JSfiddle
function deleteEmpty(obj){
for(var k in obj)
if(k == "children"){
if(obj[k]){
deleteEmpty(obj[k]);
}else{
delete obj.children;
}
}
}
for(var i=0; i< a.children.length; i++){
deleteEmpty(a.children[i])
}
delete operator is used to remove an object property.
delete operator does not returns the new object, only returns a boolean: true or false.
In the other hand, after interpreter executes var updatedjsonobj = delete myjsonobj['otherIndustry']; , updatedjsonobj variable will store a boolean
value.
How to remove Json object specific key and its value ?
You just need to know the property name in order to delete it from the object's properties.
delete myjsonobj['otherIndustry'];
let myjsonobj = {
"employeeid": "160915848",
"firstName": "tet",
"lastName": "test",
"email": "[email protected]",
"country": "Brasil",
"currentIndustry": "aaaaaaaaaaaaa",
"otherIndustry": "aaaaaaaaaaaaa",
"currentOrganization": "test",
"salary": "1234567"
}
delete myjsonobj['otherIndustry'];
console.log(myjsonobj);
If you want to remove a key when you know the value you can use Object.keys function which returns an array of a given object's own enumerable properties.
let value="test";
let myjsonobj = {
"employeeid": "160915848",
"firstName": "tet",
"lastName": "test",
"email": "[email protected]",
"country": "Brasil",
"currentIndustry": "aaaaaaaaaaaaa",
"otherIndustry": "aaaaaaaaaaaaa",
"currentOrganization": "test",
"salary": "1234567"
}
Object.keys(myjsonobj).forEach(function(key){
if (myjsonobj[key] === value) {
delete myjsonobj[key];
}
});
console.log(myjsonobj);
There are several ways to do this, lets see them one by one:
- delete method: The most common way
const myObject = {
"employeeid": "160915848",
"firstName": "tet",
"lastName": "test",
"email": "[email protected]",
"country": "Brasil",
"currentIndustry": "aaaaaaaaaaaaa",
"otherIndustry": "aaaaaaaaaaaaa",
"currentOrganization": "test",
"salary": "1234567"
};
delete myObject['currentIndustry'];
// OR delete myObject.currentIndustry;
console.log(myObject);
- By making key value undefined: Alternate & a faster way:
let myObject = {
"employeeid": "160915848",
"firstName": "tet",
"lastName": "test",
"email": "[email protected]",
"country": "Brasil",
"currentIndustry": "aaaaaaaaaaaaa",
"otherIndustry": "aaaaaaaaaaaaa",
"currentOrganization": "test",
"salary": "1234567"
};
myObject.currentIndustry = undefined;
myObject = JSON.parse(JSON.stringify(myObject));
console.log(myObject);
- With es6 spread Operator:
const myObject = {
"employeeid": "160915848",
"firstName": "tet",
"lastName": "test",
"email": "[email protected]",
"country": "Brasil",
"currentIndustry": "aaaaaaaaaaaaa",
"otherIndustry": "aaaaaaaaaaaaa",
"currentOrganization": "test",
"salary": "1234567"
};
const {currentIndustry, ...filteredObject} = myObject;
console.log(filteredObject);
Or if you can use omit() of underscore js library:
const filteredObject = _.omit(currentIndustry, 'myObject');
console.log(filteredObject);
When to use what??
If you don't wanna create a new filtered object, simply go for either option 1 or 2. Make sure you define your object with let while going with the second option as we are overriding the values. Or else you can use any of them.
hope this helps :)
Loop through the elements of the array and delete position from each.
for (var i = 0; i < $products.length; i++) {
delete $products[i].position;
}
I would use map instead of forEach.
const products = [{ foo: 'bar', position: 1 }, { foo: 'baz', position: 2}, { doo: 'daz' }];
console.log(products.map(obj => delete obj.position && obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Although it is probably less readable it makes more sense to me because you want to generate a new array applying a delete function to each element.
Other ways it can be achieved.
For Adding:
candidate["skills"] = "javascript";
For Deleting:
var skill = "javascript";
delete candidate[skill];
or
delete candidate.skills;
Removing a property of an object can be done by using the delete keyword:
candidate.delete("skills");
OR
delete candidate["skills"];
To add a property to an existing object in JS you could do the following.
candidate["skills"] = "javscript";
OR
candidate.skills = "javscript";
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete