我想一一过滤具有多个搜索条件的对象数组。
假设第一个过滤条件是“ x”,然后应用“ x + y”等。只要x或“ x + y”与对象数组匹配,就会显示匹配的结果。
如果用户要删除“ x”或“ y”,则数组应做出相应的响应。
例如:这里 x = mumbai 然后是 y = 25
var users = [{
name: 'nitin',
email: 'nitin@mail.com',
age: 25,
address: 'mumbai'
},
{
name: 'pawan',
email: 'pan@mail.com',
age: 35,
address: 'kolkata'
},
{
name: 'sudip',
email: 'sudip@mail.com',
age: 28,
address: 'mumbai'
}];
this.searchGridText = "";
this.users = this.users.filter((data) => {
const stringified = JSON.stringify(data);
const filtered = stringified.replace(/"\w+":/gm, '');
return (filtered.toLowerCase()
.indexOf(this.searchGridText.toLowerCase().trim()) !== -1);
});
当搜索条件为“孟买”时,应返回两条记录,然后搜索条件为25,则应返回一条记录。如果删除了孟买搜索条件,则仅显示与25匹配的记录。搜索条件将在文本框中输入按下Enter键后,过滤器将应用于对象数组。
答案 0 :(得分:3)
这是通过Array.filter,Array.every,Array.some和Object.values进行操作的一种方式:
var users = [{ name: 'nitin', email: 'nitin@mail.com', age: 25, address: 'mumbai' }, { name: 'pawan', email: 'pan@mail.com', age: 35, address: 'kolkata' }, { name: 'sudip', email: 'sudip@mail.com', age: 28, address: 'mumbai' } ];
let findUsers = (arr, filters) => {
let fs = filters.split('+').map(x => x.trim().toLowerCase())
return arr.filter(x => {
let vs = Object.values(x)
return fs.every(f => vs.some(v => v.toString().trim().toLowerCase().includes(f)))
})
}
console.log(findUsers(users, 'mumbai'))
console.log(findUsers(users, 'mumbai + 25'))
console.log(findUsers(users, 'sudip'))
想法是过滤并在内部获取每个对象的值,并对其进行迭代,以尝试将传递给函数过滤器的内容进行匹配。
答案 1 :(得分:1)
您可以使用相同的想法,但可以将其转变为功能,这将通过您所寻找的特征逐渐加以区分
var users = [{
name: 'nitin',
email: 'nitin@mail.com',
age: 25,
address: 'mumbai'
},
{
name: 'pawan',
email: 'pan@mail.com',
age: 35,
address: 'kolkata'
},
{
name: 'sudip',
email: 'sudip@mail.com',
age: 28,
address: 'mumbai'
}
];
this.searchGridText = "mumbai 25";
let filters = searchGridText.split(" ");
let res = [];
for (let i = 0; i < filters.length; i++) {
res = filtereds(filters[i]);
users = res;
}
function filtereds(c) {
this.users = this.users.filter((data) => {
const stringified = JSON.stringify(data);
const filtered = stringified.replace(/"\w+":/gm, '');
//console.log(c);
return (filtered.toLowerCase()
.indexOf(c.toLowerCase().trim()) !== -1);
});
return this.users;
}
console.log(users);
答案 2 :(得分:1)
创建一个将过滤键作为数组并根据这些值进行过滤的函数。您可以使用.split()
将输入的值拆分为数组,并将其传递给函数。
function searchJson(array, keys) {
return array.filter(item => {
const string = item && typeof item === 'object' && JSON.stringify(item).toLowerCase();
return keys.filter(key => string && string.includes(key.toString().toLowerCase())).length === keys.length;
})
}
var users = [{
name: 'nitin',
email: 'nitin@mail.com',
age: 25,
address: 'mumbai'
},
{
name: 'pawan',
email: 'pan@mail.com',
age: 35,
address: 'kolkata'
},
{
name: 'sudip',
email: 'sudip@mail.com',
age: 28,
address: 'mumbai'
}];
function searchJson(array, keys) {
return array.filter(item => {
const string = item && typeof item === 'object' && JSON.stringify(item).toLowerCase();
return keys.filter(key => string && string.includes(key.toString().toLowerCase())).length === keys.length;
})
}
console.log(searchJson(users, ['mumbai', 25]))
答案 3 :(得分:1)
没有字符串化,没有正则表达式,也没有问题:
function search(array, query) {
let filters = query.split("+").map(x => x.trim().toLowerCase())
return users.filter(user => {
for (let filter of filters) {
let match = false
for (let key in user) {
let value = user[key]
if (value && value.toString().toLowerCase().includes(filter)) {
match = true
break
}
}
if (!match) { return false }
}
return true
})
}
let users = [{
name: undefined,
email: 'nitin@mail.com',
age: 25,
address: 'mumbai'
}, {
name: 'pawan',
email: 'pan@mail.com',
age: 35,
address: 'kolkata'
}, {
name: 'sudip',
email: 'sudip@mail.com',
age: null,
address: 'mumbai'
}]
let result = search(users, "Mumbai + 25");
console.log(result)