如何撰寫一個可以實作這一點的 JS 回圈?
原始資料:
[
{
"firstName": "James",
"lastName": "Doe",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": "[email protected]"
}
],
"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": "[email protected]"
}
],
"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": "[email protected]"
}
],
"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": "[email protected]"
}
],
"relationship": "ex-husband",
"phones": [
{
"phoneType": "CELL",
"phoneNumber": "(111) 111-1111"
},
],
"callSequence": 0
}
]
如您所見,如果電話號碼或郵件地址為空,它應該過濾掉并洗掉完整的屬性。由于它在一個回圈中并且可以有多個物件,我不確定如何撰寫一個回圈來實作這個所需的結果。
演示: https ://jsfiddle.net/pa6ug53b/1/
uj5u.com熱心網友回復:
您可以映射陣列,然后過濾陣列內的物件以獲取特定鍵。
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: "[email protected]"
}
],
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: "[email protected]"
}
],
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);
uj5u.com熱心網友回復:
這是實作所需結果的一種可能實作方式。
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迭代資料。 - 對于電子郵件,電話“處理”陣列(洗掉空地址、電話號碼)
- 陣列的處理在一個單獨的方法 (
processObjArray) 中處理,該方法采用.filter
這是代碼片段:
const data = [{
"firstName": "James",
"lastName": "Doe",
"emails": [{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": "[email protected]"
}
],
"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": "[email protected]"
}
],
"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());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/415597.html
標籤:
下一篇:Lodash開始()
