gpt4 book ai didi

javascript - 在多个值的对象数组中搜索

转载 作者:行者123 更新时间:2023-12-05 02:41:39 25 4
gpt4 key购买 nike

我有对象数组:

[
{
"caseNumber": 123,
"patientName": "John Doe",
"technician": "Jasmin Joe",
"reader": "Jade Boe"
},
{
"caseNumber": 123,
"patientName": "John Doe",
"technician": "Jasmin Joe",
"reader": "Jade Boe"
},
{
"caseNumber": 123,
"patientName": "John Doe",
"technician": "Jasmin Joe",
"reader": "Jade Boe"
}
]

我在顶部有一个搜索栏,想从我提供的任何键中搜索任何匹配的字符串。例如:我想从 caseNumber、patientName 和 reader 进行搜索,所以我将我的过滤函数称为 filter(allCases, caseNumber, patientName, reader)我尝试使用 Angular pipers,但它只适用于前两个键。

我试过下面的代码:

this.activeCases = this.filterPipe.transform(
activeCases,
this.searchUser,
this.isCurrentUserTech ? 'readerName' : 'techName',
'patientFullName',
'caseNumber'
);

还有我的管道函数:

@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(items: any[], value: string, label: string, optionalLabel?: string, optionalLabel2?: string, optionalValue?: string): any[] {
if (!items) {
return [];
}
if (!optionalValue && !value) {
return items;
}

if ((value === '' || value == null) && (optionalValue === '' || optionalValue == null)) {
return [];
}

if (optionalValue && optionalLabel) {
this.filterByValue(value, items, label, optionalLabel, optionalValue);
} else if (optionalLabel) {
return items.filter(e => (e[optionalLabel] + ', ' + e[label]).toLowerCase().indexOf(value.toLowerCase()) > -1);
} else if (optionalLabel && label && value) {
return items.filter(item => {
let object: any = { item1: optionalLabel, item2: label };
if (optionalLabel2) {
object = { item1: optionalLabel, item2: label, item3: optionalLabel2 };
}
for (const property in object) {
if (item[object[property]] === null) {
continue;
}
if (item[object[property]].toString().toLowerCase().includes(value.toLowerCase())) {
return true;
}
}
return false;
});
} else {
return items.filter(e => {
const tempValue = e[label] ? e[label].toLowerCase() : '';
return tempValue.indexOf(value.toLowerCase()) > -1;
});
}
}

filterByValue(value, items, label, optionalLabel, optionalValue) {
if (value) {
return items.filter(e => {
const firstLabel = e[label] ? e[label].toLowerCase() : '';
const secondLabel = e[optionalLabel] ? e[optionalLabel].toLowerCase() : '';
const firstValue = value ? value.toLowerCase() : value;
const secondValue = optionalValue ? optionalValue.toLowerCase() : optionalValue;
let firstCheck = false;
let secondCheck = false;
if (firstLabel && firstValue) {
firstCheck = firstLabel.includes(firstValue);
}
if (secondLabel && secondValue) {
secondCheck = secondLabel.includes(secondValue);
}
return firstCheck && secondCheck;
});
} else {
return items.filter(e => {
const tempValue = e[optionalLabel] ? e[optionalLabel].toLowerCase() : '';
return tempValue.indexOf(optionalValue.toLowerCase()) > -1;
});
}
}
}

我错过了什么?我也可以使用 lodash

最佳答案

const data = [{
caseNumber: 123,
patientName: 'Adam',
technician: 'Jasicca',
reader: 'Potter',
},
{
caseNumber: 456,
patientName: 'John Doe',
technician: 'Kevin',
reader: 'Harry',
},
{
caseNumber: 789,
patientName: 'Ram',
technician: 'Bob',
reader: 'Jade Boe',
},
];

const input = document.querySelector('input');
const log = document.getElementById('log');

function searchArray(e) {
let filtered = [];
const input = e.target.value.toLowerCase();
if (input) {
filtered = data.filter((el) => {
return Object.values(el).some((val) =>
String(val).toLowerCase().includes(input)
);
});

log.textContent = JSON.stringify(filtered);
}
}
<input placeholder="Enter some text" name="name" onkeyup="searchArray(event)" />
<p id="log"></p>

关于javascript - 在多个值的对象数组中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68034669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com