I have an array of JSON objects. Given a search string, I want to filter the array for only those objects which have that string as a substring of one of their properties. How do I do this efficiently?
-
5provide a minimal reproducible example along with an example of search string and the JSON itself.zer00ne– zer00ne2017-12-05 12:49:38 +00:00Commented Dec 5, 2017 at 12:49
-
1Does this answer your question? JS search in object valuesHeretic Monkey– Heretic Monkey2021-08-11 13:24:59 +00:00Commented Aug 11, 2021 at 13:24
8 Answers
Assuming you want to find the substring in the property value, you can use the following code:
const arr = [
{a:'abc', b:'efg', c:'hij'},
{a:'abc', b:'efg', c:'hij'},
{a:'123', b:'456', c:'789'},
];
const search = 'a';
const res = arr.filter(obj => Object.values(obj).some(val => val.includes(search)));
console.log(res);
If you want to search the property name, use Object.keys instead of Object.values.
Please note that Object.values is a feature of ES2017.
4 Comments
Works like a charm:
data.filter((obj) =>
JSON.stringify(obj).toLowerCase().includes(query.toLowerCase())
)
1 Comment
query.Maybe you want to do something like this:
var list = [
{var1: "value1", var2: "value2", var3: "value3"},
{var1: "value4", var2: "value5", var3: "value6"},
{var1: "value4", var2: "value3", var3: "value2"},
{var1: "value2", var2: "value8", var3: "value6"},
{var1: "value1", var2: "value7", var3: "value7"},
{var1: "value1", var2: "value6", var3: "value2"},
];
var searchString = "value2";
var newList = list.filter(element => {
for (var property in element) {
if (element.hasOwnProperty(property)) {
if(element[property] == searchString) {
return true;
}
}
}
});
console.log(newList);
Comments
You could use the filter method to filter the array and then use the Object.keys function to get an array of keys on the current object. You can then loop over each key and check if it has the substring (I added some guards to protect against calling .indexOf on an identifier without that method.)
const search = 'xyz';
const data = yourDataFunction();
const filteredData = data.filter(item => {
let found = false;
Object.keys(item).forEach(key => {
if (item[key] && item[key].indexOf && item[key].indexOf(search) > -1) {
found = true;
}
});
return found;
});
Comments
I found out a method that works for me. I went further to convert the query string to lower case:
const data = [
{a: 'ekom', b: 'obong'},
{a: 'fin', b: 'barr'}
];
const queryString = 'fi';
const result = data.filter((obj) => JSON.stringify(obj).toLowerCase().includes(queryString.toString().toLowerCase()));
console.log('Result: ', result);
Comments
A little improvement to str's answer, for better performance;
const arr = [
{a:'abc', b:'efg', c:'hij'},
{a:'abc', b:'efg', c:'hij'},
{a:'123', b:'456', c:'789'},
];
const search = 'a';
const res = arr.filter(obj => Object.values(obj).some(val => val.indexOf(search) >= 0));
console.log(res);
Comments
Thanks, @str to contribute. I just update it a little bit because it's not working a newer version of js is in strict mode. We need to covert the final val to string before checking includes method.
const arr = [
{a: 'abcd', b: 'cdaf'},
{a: 'xyz', b: 'axcfd'}
];
const searchValue = 'a';
const newArr = arr.filter(obj => Object.values(obj).some(val => val.toString().includes(searchValue)));