从 javascript 中的对象数组中提取匹配值

我有一个对象数组,我正在尝试创建一个过滤器,用户在其中键入几个字母并获取所有匹配记录的列表。

users = [{office: "J Limited", contact: {first_name: "James", last_name: "Wilson", address: Canada}},{office: "Q Limited", contact: {first_name: "Quin", last_name: "Ross", address: Australia}},{office: "N Limited", contact: {first_name: "Nancy", last_name: "Mathew"}, address: "England"}]

我有一个文本字段,用户在其中输入以获取结果,并假设用户输入ja,因此结果应搜索字段 office、contact first_name 和 last_name,如果任何此类字段包含匹配字母,则应为请求,就像在本例中一样,结果输出应为

J Limited, James, Wilson

请帮助我实现这一目标。


阿波罗的战车
浏览 301回答 4
4回答

小唯快跑啊

您可以循环遍历数组并搜索对象。users = [{&nbsp; office: "J Limited",&nbsp; contact: {&nbsp; &nbsp; first_name: "James",&nbsp; &nbsp; last_name: "Wilson",&nbsp; &nbsp; address: "Canada"&nbsp; }}, {&nbsp; office: "Q Limited",&nbsp; contact: {&nbsp; &nbsp; first_name: "Quin",&nbsp; &nbsp; last_name: "Ross",&nbsp; &nbsp; address: "Australia"&nbsp; }}, {&nbsp; office: "N Limited",&nbsp; contact: {&nbsp; &nbsp; first_name: "Nancy",&nbsp; &nbsp; last_name: "Mathew"&nbsp; },&nbsp; address: "England"},{&nbsp; office: "J Limited",&nbsp; contact: {&nbsp; &nbsp; first_name: "Jacob",&nbsp; &nbsp; last_name: "Wilson",&nbsp; &nbsp; address: "Canada"&nbsp; }}]function search(searchKey) {&nbsp; searchKey = searchKey.toLowerCase();&nbsp; results = [];&nbsp; for (var i = 0; i < users.length; i++) {&nbsp; &nbsp; if (users[i].contact.first_name.toLowerCase().includes(searchKey) || users[i].contact.last_name.toLowerCase().includes(searchKey) || users[i].office.toLowerCase().includes(searchKey)) {&nbsp; &nbsp; &nbsp; results.push(users[i]);&nbsp; &nbsp; }&nbsp; }&nbsp; return results;}var resultObject = search("ja");if (resultObject) {&nbsp; for(i in resultObject){&nbsp; &nbsp; console.log(resultObject[i].office, resultObject[i].contact.first_name, resultObject[i].contact.last_name)&nbsp; }}

翻过高山走不出你

要获取匹配的用户,您可以执行以下操作:const searchString = 'ja'const matchingUsers = users.filter(user =>&nbsp;&nbsp; user.office.contains(searchString) ||&nbsp;&nbsp; user.contact.first_name.contains(searchString) ||&nbsp;&nbsp; user.contact.last_name.contains(searchString))然后您可以按照自己喜欢的方式设置匹配用户列表的格式编辑:contains可能不适用于某些 JS 版本(适用于 Chrome),因此替换contains为includes:const searchString = 'ja'const matchingUsers = users.filter(user =>&nbsp;&nbsp; user.office.includes(searchString) ||&nbsp;&nbsp; user.contact.first_name.includes(searchString) ||&nbsp;&nbsp; user.contact.last_name.includes(searchString))

不负相思意

let users = [{&nbsp; &nbsp; office: "J Limited",&nbsp; &nbsp; contact: { first_name: "James", last_name: "Wilson", address: "Canada" }}, {&nbsp; &nbsp; office: "Q Limited",&nbsp; &nbsp; contact: { first_name: "Quin", last_name: "Ross", address: "Australia" }}, {&nbsp; &nbsp; office: "N Limited",&nbsp; &nbsp; contact: { first_name: "Nancy", last_name: "Mathew", address: "England"},}];// ig: i for case-insensitive, g for globalconst regEx = new RegExp('ja', 'ig');const result = users.filter(&nbsp; &nbsp; each =>&nbsp; &nbsp; &nbsp; &nbsp; each.office.match(regEx) ||&nbsp; &nbsp; &nbsp; &nbsp; each.contact.first_name.match(regEx) ||&nbsp; &nbsp; &nbsp; &nbsp; each.contact.last_name.match(regEx)).map(&nbsp; &nbsp; each => [&nbsp; &nbsp; &nbsp; &nbsp; each.office,&nbsp; &nbsp; &nbsp; &nbsp; each.contact.first_name,&nbsp; &nbsp; &nbsp; &nbsp; each.contact.last_name&nbsp; &nbsp; ]);console.log(result);

烙印99

const filterUser = users.filter(user =>{ return user.office.toLowerCase().includes((searchField).toLowerCase()) });您的 searchField 是输入的值,现在可以使用 filteruser,因为它将返回搜索中包含办公室名称的用户,或者如果在 searchField 中未输入任何内容,则返回全部。您现在可以通过 filterUser 进行映射,以便能够使用包含输入值(即您的 searchField)的用户。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript