如何以不可变的方式过滤作为对象属性的数组? 对于前。
public transform(contactGroups: ContactGroup[], searchText: string): ContactGroup[] {
if (!contactGroups) {
return [];
}
if (searchText === undefined) {
return contactGroups;
}
return contactGroups.filter((contactGroup: ContactGroup) => {
return contactGroup.contacts.filter((contact: Contact) => {
return contact.displayName && contact.displayName.toLowerCase().includes(searchText.toLowerCase())
}).length > 0;
});
}
在上面的示例中,contactGroup.contacts包含数组中的所有项目,但不包含过滤后的结果到期对象引用。
任何帮助将不胜感激。 谢谢。
function transform(contactGroups, searchText) {
if (!contactGroups) {
return [];
}
if (searchText === undefined) {
return contactGroups;
}
return contactGroups.filter(function (contactGroup) {
return contactGroup.contacts.filter(function (contact) {
return (contact.displayName && contact.displayName.toLowerCase().includes(searchText.toLowerCase()));
}).length > 0;
});
};
var contactGroups = [{
"letter":"S",
"contacts":[
{
"id":"173",
"rawId":null,
"displayName":"sam",
"name":{
"givenName":"sam",
"formatted":"sam"
},
"nickname":null,
"phoneNumbers":null,
"emails":[
{
"id":"955",
"pref":false,
"value":"sam@xyz.com",
"type":"other"
}
],
"addresses":null,
"ims":null,
"organizations":null,
"birthday":null,
"note":"",
"photos":null,
"categories":null,
"urls":null
},
{
"id":"1717",
"rawId":null,
"displayName":"Sat33",
"name":{
"givenName":"Sat33",
"formatted":"Sat33 "
},
"nickname":null,
"phoneNumbers":[
{
"id":"5521",
"pref":false,
"value":"1133",
"type":"work"
}
],
"emails":null,
"addresses":null,
"ims":null,
"organizations":null,
"birthday":null,
"note":null,
"photos":null,
"categories":null,
"urls":null
},
{
"id":"1712",
"rawId":null,
"displayName":"Server1234",
"name":{
"givenName":"Server1234",
"formatted":"Server1234 "
},
"nickname":null,
"phoneNumbers":[
{
"id":"5509",
"pref":false,
"value":"1234",
"type":"mobile"
}
],
"emails":null,
"addresses":null,
"ims":null,
"organizations":null,
"birthday":null,
"note":null,
"photos":null,
"categories":null,
"urls":null
}
]
}]
console.log(transform(contactGroups, 'ver'))
它应该返回contactGroup只有联系对象'Server1234',因为搜索到的字符串是'ver'但它仍然返回contactGroup与所有联系对象。
答案 0 :(得分:2)
你可以尝试这个解决方案
class ContactGroup {
contacts: Contact[]
}
class Contact {
displayName: string
}
function transform(contactGroups: ContactGroup[], searchText: string): ContactGroup[] {
if (!contactGroups) {
return [];
}
if (searchText === undefined) {
return contactGroups;
}
return contactGroups.map((contactGroup: ContactGroup) => {
return {
...contactGroup,
contacts: contactGroup.contacts.filter((contact: Contact) => {
return contact.displayName && contact.displayName.toLowerCase().includes(searchText.toLowerCase())
})
}
}).filter((contactGroup: ContactGroup) => {
return contactGroup.contacts.length > 0
})
}
答案 1 :(得分:0)
您可以清空传递的数组并将过滤后的元素推入其中。以下是代码的简化版本:
function transform(contactGroups, searchText) {
var filtered = contactGroups.filter(e => e.name.includes(searchText));
contactGroups.splice(0, contactGroups.length);
contactGroups.push(...filtered);
return filtered; // <-- if still needed
}
var contacts = [{name: 'abc'}, {name: 'abcc'}, {name: 'xyz'}];
transform(contacts, 'abc');
console.log(contacts);
&#13;