As the title states, I'm attempting to essentially clean an object to prepare it for POSTing to an API.
The API endpoint does not allow for the empty arrays.
So let's say I have a POST object that looks like this -
{
"Addresses": [
{}
],
"Phones": [
{}
],
"FirstName": "Foo",
"LastName": "Bar",
"EmailAddress": "foobar@internet.com",
"Name": "FooBar"
}
I believe I clean empty objects from the arrays using something like this -
var addrArr = input.Addresses;
var phoneArr = input.Phones;
var newAddrArray = addrArr.filter(value => Object.keys(value).length !== 0);
var newPhoneArray = phoneArr.filter(value => Object.keys(value).length !== 0);
return {
Addresses : newAddrArray,
Phones: newPhoneArray
}
This removes the empty objects from the array. However, this leaves two empty arrays. My thoughts are it would be best to clean the empty objects out, then determine if these arrays are empty, and if they are, (where I need help) remove them from the parent object.
Any help on how to handle this, or, how to do this more efficiently is much appreciated.