I want to search matching value objects from a given array depending on searchString. e.g., the search string would be 'Object 0'.
var objArray = [
{ id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
{ id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
{ id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
{ id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 1' },
{ id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];
Now I want to search in the array with Object as a value. Means it should return me 0, 1 & 3 object
Help will be appreciated
4 Answers 4
You can loop through each object in the array, get the keys for each object to prevent yourself from dynamic properties in the object, and then check if the value for that key has Object as a substring or not. Note that obj[key].toString() in the below code. toString() is used because your id has integer value and indexOf() works on string value. Thus, using that will prevent from error.
var objArray = [
{ id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
{ id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
{ id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
{ id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 1' },
{ id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];
var res = [];
objArray.filter((obj) => {
Object.keys(obj).forEach((key)=>{
if(obj[key].toString().indexOf('Object') !== -1){
res.push(obj);
}
});
});
console.log(res);
Comments
You could do that with the array.filter method and a loop over all properties within this filter method.
var objArray = [
{ id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
{ id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
{ id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
{ id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0' },
{ id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];
var filteredArray = objArray.filter((obj) => {
for (var key in obj) {
if (obj[key] === 'Object 0') {
return true;
}
}
return false;
});
3 Comments
== doesn't ignore case. You have to convert your strings into lowercase for comparison. Just replace the if-statement with this: if (obj[key].toLowerCase() === 'object 0'). For other comparisons you may have to use regex.if (obj[key].match(/object/i)) { ....You can do this by using filter() in Arrays.
Here's a sample of what you're looking for
var objArray = [
{ id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
{ id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
{ id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
{ id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0' },
{ id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];
var resultArray = objArray.filter(function (d) {
return Object.values(d).indexOf('Object 0') != -1
});
The value of d in the filter() method takes each object from your array of object. Object.values() returns an array of the values associated with each of the keys in the object. The indexOf() essentially checks if the string you want to match is available in the array. In case it matches, the result is put in resultArray else it's ignored. The final resultArray looks as follows:
[
{id: 0, name: "Object 0", otherProp: "321", secondVal: "stack"},
{id: 1, name: "O1", otherProp: "Object 0", secondVal: "Overflow"},
{id: 3, name: "Almost There", otherProp: "046", secondVal: "Object 0"}
]
Comments
You can use array.filter() and array.includes(), Object.values() to do this.
var objArray = [
{id: 0, name: 'Object 0', otherProp: '321', secondVal: 'stack'},
{id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow'},
{id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context'},
{id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0'},
{id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test'},
];
const result = objArray.filter((object) => {
const values = Object.values(object);
return values.includes('Object 0');
});
console.log(result);
objArray.filter(item => Object.values(item).includes('Object 0'));array.filter