JavaScript: 当值为空字符串时,删除嵌套在另一个对象中的对象的键

JavaScript: delete an Object's key which is nested inside an another Object when value is an empty string

如何写一个JS循环来实现这个?

原始数据:

[
{
    "firstName": "James",
    "lastName": "Doe",
    "emails": [
        {
            "emailType": "WORK",
            "address": ""
        },
        {
            "emailType": "PERSONAL",
            "address": "test2@gmail.com"
        }
    ],
    "relationship": "boyfriend",
    "phones": [
        {
            "phoneType": "HOME",
            "phoneNumber": "(514) 888-9999"
        },
        {
            "phoneType": "CELL",
            "phoneNumber": "(123) 123-4567"
        },
        {
            "phoneType": "WORK",
            "phoneNumber": "(415) 875-9999 "
        }
    ],
    "callSequence": 0
},
{
    "firstName": "John",
    "lastName": "Doe",
    "emails": [
        {
            "emailType": "WORK",
            "address": ""
        },
        {
            "emailType": "PERSONAL",
            "address": "test2@gmail.com"
        }
    ],
    "relationship": "ex-husband",
    "phones": [
        {
            "phoneType": "HOME",
            "phoneNumber": ""
        },
        {
            "phoneType": "CELL",
            "phoneNumber": "(111) 111-1111"
        },
        {
            "phoneType": "WORK",
            "phoneNumber": ""
        }
    ],
    "callSequence": 0
}
]

预期结果:

[
{
    "firstName": "James",
    "lastName": "Doe",
    "emails": [
        {
            "emailType": "PERSONAL",
            "address": "test2@gmail.com"
        }
    ],
    "relationship": "boyfriend",
    "phones": [
        {
            "phoneType": "HOME",
            "phoneNumber": "(514) 888-9999"
        },
        {
            "phoneType": "CELL",
            "phoneNumber": "(123) 123-4567"
        },
        {
            "phoneType": "WORK",
            "phoneNumber": "(415) 875-9999 "
        }
    ],
    "callSequence": 0
},
{
    "firstName": "John",
    "lastName": "Doe",
    "emails": [
        {
            "emailType": "PERSONAL",
            "address": "test2@gmail.com"
        }
    ],
    "relationship": "ex-husband",
    "phones": [
        {
            "phoneType": "CELL",
            "phoneNumber": "(111) 111-1111"
        },
    ],
    "callSequence": 0
}
]

如您所见,电话号码或邮件地址是否为空,它应该过滤掉并删除完整的 属性。由于是在一个循环中,而且可以有多个对象,所以我不知道如何写一个循环来达到这个要求的结果。

演示: https://jsfiddle.net/pa6ug53b/1/

这是实现预期结果的一种可能实施方式。

const processObjArray = (arr = [], colName = 'phoneNumber') => (
    arr.filter(o => o[colName] && o[colName].length > 0)
);

const removeEmptyValues = (arr = data) => arr.map(o => ({
  ...o,
  emails: processObjArray(o.emails, 'address'),
  phones: processObjArray(o.phones)
}));

console.log(removeEmptyValues());

说明

  1. 使用 .map 迭代数据。
  2. 对于电子邮件、电话 'process' 数组(删除空地址、电话号码)
  3. 数组的处理是在一个单独的方法 (processObjArray) 中处理的,该方法使用 .filter

这是代码片段:

const data = [{
    "firstName": "James",
    "lastName": "Doe",
    "emails": [{
        "emailType": "WORK",
        "address": ""
      },
      {
        "emailType": "PERSONAL",
        "address": "test2@gmail.com"
      }
    ],
    "relationship": "boyfriend",
    "phones": [{
        "phoneType": "HOME",
        "phoneNumber": "(514) 888-9999"
      },
      {
        "phoneType": "CELL",
        "phoneNumber": "(123) 123-4567"
      },
      {
        "phoneType": "WORK",
        "phoneNumber": "(415) 875-9999 "
      }
    ],
    "callSequence": 0
  },
  {
    "firstName": "John",
    "lastName": "Doe",
    "emails": [{
        "emailType": "WORK",
        "address": ""
      },
      {
        "emailType": "PERSONAL",
        "address": "test2@gmail.com"
      }
    ],
    "relationship": "ex-husband",
    "phones": [{
        "phoneType": "HOME",
        "phoneNumber": ""
      },
      {
        "phoneType": "CELL",
        "phoneNumber": "(111) 111-1111"
      },
      {
        "phoneType": "WORK",
        "phoneNumber": ""
      }
    ],
    "callSequence": 0
  }
];


const processObjArray = (arr = [], colName = 'phoneNumber') => (arr.filter(o => o[colName] && o[colName].length > 0));

const removeEmptyValues = (arr = data) => arr.map(o => ({ ...o,
  emails: processObjArray(o.emails, 'address'),
  phones: processObjArray(o.phones)
}));

console.log(removeEmptyValues());

您可以map over the array and then filter数组中的对象作为特定键。

const result = yourArray.map((obj) => ({
  ...obj,
  emails: obj.emails.filter((emailObj) => emailObj.address),
  phones: obj.phones.filter((phoneObj) => phoneObj.phoneNumber)
}));

const yourArray = [
  {
    firstName: "James",
    lastName: "Doe",
    emails: [
      {
        emailType: "WORK",
        address: ""
      },
      {
        emailType: "PERSONAL",
        address: "test2@gmail.com"
      }
    ],
    relationship: "boyfriend",
    phones: [
      {
        phoneType: "HOME",
        phoneNumber: "(514) 888-9999"
      },
      {
        phoneType: "CELL",
        phoneNumber: "(123) 123-4567"
      },
      {
        phoneType: "WORK",
        phoneNumber: "(415) 875-9999 "
      }
    ],
    callSequence: 0
  },
  {
    firstName: "John",
    lastName: "Doe",
    emails: [
      {
        emailType: "WORK",
        address: ""
      },
      {
        emailType: "PERSONAL",
        address: "test2@gmail.com"
      }
    ],
    relationship: "ex-husband",
    phones: [
      {
        phoneType: "HOME",
        phoneNumber: ""
      },
      {
        phoneType: "CELL",
        phoneNumber: "(111) 111-1111"
      },
      {
        phoneType: "WORK",
        phoneNumber: ""
      }
    ],
    callSequence: 0
  }
];

const result = yourArray.map((obj) => ({
  ...obj,
  emails: obj.emails.filter((emailObj) => emailObj.address),
  phones: obj.phones.filter((phoneObj) => phoneObj.phoneNumber)
}));

console.log(result);