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])
}
You can try following code:
let output = JSON.parse(JSON.stringify(input));
output.careerLevelGroups.forEach(group => delete group.careerLevels);
Use array Map method to iterate the careerLevelGroups array and then delete the careerLevels property of an object :
var jsonObj = {
"careerLevelGroups": [{
"201801": 58,
"201802": 74,
"careerLevel": "Analyst",
"careerLevels": [{
"201801": 29,
"201802": 37,
"careerID": "10000100"
},
{
"201801": 29,
"201802": 37,
"careerID": "10000110"
}
]
},
{
"201801": 58,
"201802": 74,
"careerLevel": "Consultant",
"careerLevels": [{
"201801": 29,
"201802": 37,
"careerID": "10000080"
},
{
"201801": 29,
"201802": 37,
"careerID": "10000090"
}
]
}
]
};
var res = jsonObj.careerLevelGroups.map(obj => {
delete obj.careerLevels
return obj;
});
console.log(res);
You'll have to iterate over the addressesFound array, and in each object iterate over the keys of that object, compare if that key exists in addressNonRequired array and remove it, if applicable.
var addressesFound = [{
"addr_id": "41d86d46-8b19-4f4e-be03-f9915ef4947b",
"addr_type": "postal",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": false,
"addr_international": false,
"addr_autocomplete_id": null
},
{
"addr_id": "b18c2ca6-29cf-4114-9067-b37fd3394638",
"addr_type": "residential",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": true,
"addr_international": true,
"addr_autocomplete_id": "string"
}
];
const addressNonRequired = ["addr_linkid_usr", "addr_created", "addr_updated"];
var updatedAddresses = addressesFound.map(function(address) {
Object.keys(address).forEach(function(key) { // For each address object, iterate over the keys
if (addressNonRequired.includes(key)) {
delete address[key]; // Check if the key is present in the addressNonRequired array. Delete if present
}
});
return address;
});
console.log(updatedAddresses);
I always use this very often when filtering an object or an array of objects.
I usually have these in my helpers.ts file. They also make sure that the keys you are filtering does exist in the object and it also has autocomplete.
export function except<T, K extends keyof T>(data: T, keys: Array<K>) {
const copy = { ...data };
for (const key of keys) {
if (key in copy) {
delete copy[key];
}
}
return copy;
}
export function exceptMany<T, K extends keyof T>(data: Array<T>, keys: Array<K>) {
return [...data].map((item) => except(item, keys));
}
Then to filter an array of objects, we do:
const data = [
{
"addr_id": "41d86d46-8b19-4f4e-be03-f9915ef4947b",
"addr_type": "postal",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": false,
"addr_international": false,
"addr_autocomplete_id": null
},
{
"addr_id": "b18c2ca6-29cf-4114-9067-b37fd3394638",
"addr_type": "residential",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": true,
"addr_international": true,
"addr_autocomplete_id": "string"
}
];
const results = exceptMany(data, ['addr_linkid_usr', 'addr_created', 'addr_updated']);
Filtering keys from just one object is also possible:
const single = {
"addr_id": "41d86d46-8b19-4f4e-be03-f9915ef4947b",
"addr_type": "postal",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": false,
"addr_international": false,
"addr_autocomplete_id": null
};
const filteredSingle = except(single, ['addr_linkid_usr', 'addr_created', 'addr_updated']);
var 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.