I am really surprised I haven\'t been able to find anything related to my question. I am looking for a fast way to filter my array of objects based on a user text input.
Well, you can use any lodash's collection related function to object and it will iterate over values. Also you can use directly _.toLower
to any string or number or Boolean and it will handle all the corner cases, so if you want a easier solution, and well structured code then here you go:
data.filter(o=>_.some(o, v => _.toLower(v).indexOf('s')>-1))
Here is the working example:
let data = [{
"id": 1,
"first_name": "Jean",
"last_name": "Owens",
"email": "jowens0@google.ru",
"gender": "Female"
}, {
"id": 2,
"first_name": "Marie",
"last_name": "Morris",
"email": "mmorris1@engadget.com",
"gender": "Female"
}, {
"id": 3,
"first_name": "Larry",
"last_name": "Wallace",
"email": "lwallace2@example.com",
"gender": "Male"
}];
var sTxt = 's';
var res = data.filter(o=>_.some(o, v =>_.toLower(v).indexOf(sTxt)>-1))
console.log('Result: ', JSON.stringify(res,null,' '));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
You could use Object.keys()
and some()
instead.
let data = [{
"id": 1,
"first_name": "Jean",
"last_name": "Owens",
"email": "jowens0@google.ru",
"gender": "Female"
}, {
"id": 2,
"first_name": "Marie",
"last_name": "Morris",
"email": "mmorris1@engadget.com",
"gender": "Female"
}, {
"id": 3,
"first_name": "Larry",
"last_name": "Wallace",
"email": "lwallace2@example.com",
"gender": "Male"
}];
var result = data.filter(function(o) {
return Object.keys(o).some(function(k) {
return o[k].toString().toLowerCase().indexOf('s') != -1;
})
})
console.log(result)
You could use Object.keys and omit hasOwnProperty
.
This solution features arrow functions.
let data = [{ "id": 1, "first_name": "Jean", "last_name": "Owens", "email": "jowens0@google.ru", "gender": "Female" }, { "id": 2, "first_name": "Marie", "last_name": "Morris", "email": "mmorris1@engadget.com", "gender": "Female" }, { "id": 3, "first_name": "Larry", "last_name": "Wallace", "email": "lwallace2@example.com", "gender": "Male" }],
searchText = "s",
result = data.filter(o =>
Object.keys(o).some(k =>
o[k].toString().toLowerCase().indexOf(searchText) !== -1));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
For a better performance, you could store the keys in advance and iterate the keys, without using Object.keys
for all objects in the array, if they are uniform.
let data = [{ "id": 1, "first_name": "Jean", "last_name": "Owens", "email": "jowens0@google.ru", "gender": "Female" }, { "id": 2, "first_name": "Marie", "last_name": "Morris", "email": "mmorris1@engadget.com", "gender": "Female" }, { "id": 3, "first_name": "Larry", "last_name": "Wallace", "email": "lwallace2@example.com", "gender": "Male" }],
keys = Object.keys(data[0]),
searchText = "s",
result = data.filter(o =>
keys.some(k =>
o[k].toString().toLowerCase().indexOf(searchText) !== -1));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }