【发布时间】:2022-10-04 17:24:24
【问题描述】:
我想在匹配多个参数的列表中查找对象。
我的对象就像
interface Member {
name: string,
statut: string,
email: string,
number: string,
date: string,
title: string,
company: string,
id: string,
}
我有一个数组,其中包含我必须搜索的所有参数我的参数
let myArgs: Array<string> = parseValue()
console.log("searching", myArgs)
let toSearch = list //list of object I have to seach
let found: Array<any> = []
myArgs.forEach((argX) => {
console.log("toSearch = ", toSearch, argX)
toSearch.forEach((elem) => { // check each element in toSeach for argx
for (const e in elem) { // check every properties of my object (I do want to search in all of them)
if (elem[e] !== undefined && elem[e] !== elem.id) { // make sure the elem is not undefined or equal to id
if (elem[e].toString().toLowerCase().includes(argX)) { //if one of the properties contain my arg
if (found.indexOf(elem) < 0) { // I check if it is no already found
found.push(elem) // i push it into my found list
console.log("found ", argX, " in ", elem[e])
}
break; // i go for the next object in the list
}
}
}
})
console.log("found", found)
toSearch = found; // I set my found array as the one to search for the next arg
found = [] // and I reset the found array
})
ret = toSearch // and at the end my to search array is suppose to contain the objects that match all my arguments.
console.log("ret",ret);
它在我有一个参数时有效,但只要我输入 2+ 个参数,它就不会返回所有匹配的参数
正如你在这张图片上看到的,我有所有被标记为 eki:enter image description here 的人
但是当我想搜索有 eki 和 al 的人时,只有一个出现:enter image description here
正如您在第一张图片中看到的那样,有更多人与这 2 个 args 匹配
我希望我的解释足够清楚
谢谢,
【问题讨论】:
标签: javascript sorting object