简体   繁体   English

从数组中删除空对象,然后如果结果数组为空,则从父对象中删除

[英]Remove Empty Objects from Array, then if the resulting array is empty, remove from parent object

As the title states, I'm attempting to essentially clean an object to prepare it for POSTing to an API.正如标题所述,我试图从本质上清理一个对象,以准备将其发布到 API。

The API endpoint does not allow for the empty arrays. API 端点不允许使用空数组。

So let's say I have a POST object that looks like this -所以假设我有一个看起来像这样的 POST 对象 -

{
  "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.非常感谢有关如何处理此问题或如何更有效地执行此操作的任何帮助。

To make it generic (for arbitrary nested data structures), you could use a recursive solution so that there is some cascading effect after you clean out an empty object, making the wrapping object empty, ...etc.为了使其通用(对于任意嵌套数据结构),您可以使用递归解决方案,以便在清除空对象后有一些级联效果,使包装对象为空等。

Assuming you also want to remove undefined as values, then return undefined for anything that is not primitive and has no keys, and let the caller remove keys that so receive undefined values.假设您还想删除undefined作为值,然后为任何非原始且没有键的内容返回undefined ,并让调用者删除接收undefined值的键。

Code:代码:

 function clean(obj) { if (Object(obj) !== obj) return obj; // primitives are kept obj = Array.isArray(obj) ? obj.map(clean).filter(v => v !== undefined) : Object.fromEntries( Object.entries(obj).map(([k, v]) => [k, clean(v)]) .filter(([_, v]) => v !== undefined) ); return Object.keys(obj).length ? obj : undefined; } // Demo let input = { "Addresses": [{}], "Phones": [{}], "FirstName": "Foo", "LastName": "Bar", "EmailAddress": "foobar@internet.com", "Name": "FooBar" }; console.log(clean(input));

Is the following what you are asking for?以下是您所要求的吗?

const addrArr = input.addrArr;
const phoneArr = input.phoneArr;
  
const newAddrArray = addrArr.filter(value => Object.keys(value).length !== 0);
const newPhoneArray = phoneArr.filter(value => Object.keys(value).length !== 0);

const r = {};
if (newAddrArray.length > 0) r.Addresses = newAddrArray;
if (newPhoneArray.length > 0) r.Phones = newPhoneArray;

return r;

Seems strange that the API wouldn't support empty arrays. API 不支持空数组似乎很奇怪。 Regardless, this is definitely possible to work around.无论如何,这绝对可以解决。

const payload = {
  "Addresses": [
    {}
  ],
  "Phones": [
    {}
  ],
  "FirstName": "Foo",
  "LastName": "Bar",
  "EmailAddress": "foobar@internet.com",
  "Name": "FooBar"
};

let cleanedPayload = {
  "FirstName": payload.FirstName,
  "LastName": payload.LastName,
  "EmailAddress": "foobar@internet.com",
  "Name": "FooBar"
};

const isNotEmpty = obj => Object.keys(obj).length !== 0;

const hasPhoneNumbers = payload.Phones.filter(isNotEmpty).length > 0;
const hasAddresses = payload.Addresses.filter(isNotEmpty).length > 0;

if(hasPhoneNumbers) {
  cleanedPayload.Phones = payload.Phones;
}
if(hasAddresses) {
  cleanedPayload.Addresses = payload.Addresses;
}

// pseduo-code
api.post('/my/url', cleanedPayload);

To help clean up this code a bit you can use a library like https://ramdajs.com/ or https://lodash.com/ , but definitely not necessary.为了帮助清理此代码,您可以使用像https://ramdajs.com/https://lodash.com/这样的库,但绝对没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM