简体   繁体   English

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

[英]Extract matching values from array of objects in javascript

I have an array of objects and I am trying to create a filter where a user types few letters and gets the list of all the matching records.我有一个对象数组,我正在尝试创建一个过滤器,其中用户键入几个字母并获取所有匹配记录的列表。

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"}]

I have a textfield where user types to get the results and lets suppose user types ja so the result should search fields office, contact first_name and last_name and if any such field contains matching letter should be the request like in this case the result output should be我有一个文本字段,用户在其中键入以获取结果,并假设用户键入ja,因此结果应该搜索字段 office、contact first_name 和 last_name,如果任何此类字段包含匹配的字母,则应该是请求,在这种情况下,结果输出应该是

J Limited, James, Wilson

Please help me achieve this.请帮助我实现这一目标。

To get the matching users, you can do something like:要获得匹配的用户,您可以执行以下操作:

const searchString = 'ja'

const matchingUsers = users.filter(user => 
  user.office.contains(searchString) || 
  user.contact.first_name.contains(searchString) || 
  user.contact.last_name.contains(searchString)
)

And then you can format that list of matching users however you like然后您可以随意格式化匹配用户列表

EDIT: contains may not work on some JS versions (works on Chrome), so replace contains with includes :编辑: contains可能无法在一些JS版本的工作(在Chrome作品),所以更换containsincludes

const searchString = 'ja'

const matchingUsers = users.filter(user => 
  user.office.includes(searchString) || 
  user.contact.first_name.includes(searchString) || 
  user.contact.last_name.includes(searchString)
)
let 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"},
}];


// ig: i for case-insensitive, g for global
const regEx = new RegExp('ja', 'ig');

const result = users.filter(
    each =>
        each.office.match(regEx) ||
        each.contact.first_name.match(regEx) ||
        each.contact.last_name.match(regEx)
)
.map(
    each => [
        each.office,
        each.contact.first_name,
        each.contact.last_name
    ]
);

console.log(result);

You can loop through the array and search for objects.您可以遍历数组并搜索对象。

 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" }, { office: "J Limited", contact: { first_name: "Jacob", last_name: "Wilson", address: "Canada" } } ] function search(searchKey) { searchKey = searchKey.toLowerCase(); results = []; for (var i = 0; i < users.length; i++) { if (users[i].contact.first_name.toLowerCase().includes(searchKey) || users[i].contact.last_name.toLowerCase().includes(searchKey) || users[i].office.toLowerCase().includes(searchKey)) { results.push(users[i]); } } return results; } var resultObject = search("ja"); if (resultObject) { for(i in resultObject){ console.log(resultObject[i].office, resultObject[i].contact.first_name, resultObject[i].contact.last_name) } }

const filterUser = users.filter(user =>{ return user.office.toLowerCase().includes((searchField).toLowerCase()) }); const filterUser = users.filter(user =>{ return user.office.toLowerCase().includes((searchField).toLowerCase()) });

your searchField is the value of input and the filteruser can now be used as it will return the user with office name contain in the search or return all if nothing is enter in searchField.您的 searchField 是 input 的值,现在可以使用 filteruser ,因为它将返回搜索中包含 office 名称的用户,或者如果 searchField 中未输入任何内容则返回 all 。

You can now map through filterUser to be able to use the user contain the input value which is your searchField.您现在可以通过 filterUser 进行映射,以便能够使用包含作为您的搜索字段的输入值的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从嵌套在数组 JAVASCRIPT 内的对象中提取值 - how to extract values from an objects nested inside array JAVASCRIPT javascript从对象数组中提取键的相同值 - javascript extract same values of keys from array of objects 匹配 JavaScript 中另一个数组的对应值后,如何根据一个数组中的值提取数据? - How to Extract data based on the values in one array after matching the corresponding values from another array in JavaScript? 从Javascript数组中提取值 - Extract values from Javascript Array 从一个 Array 对象中提取键值,并使用带下划线 javascript 的提取值从其他对象中过滤 - Extract key values from one Array object and filter from other objects with the extracted values with underscore javascript JavaScript:从对象数组中的对象数组中提取属性值作为数组 - JavaScript: from an array of objects of an array of objects, extract value of a property as array 从JSON对象数组中的字段中提取值 - Extract values from fields in an array of JSON objects 如何仅从对象数组中提取值并使用 react 和 javascript 将其放入数组中? - How to extract only values from array of objects and put it to an array using react and javascript? 将对象数组与值数组匹配 - Matching array of objects with array of values 在javascript中匹配数组中的值 - Matching for the Values in an array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM