Is there a way to filter an array of objects by a particular value that could be in any property?
Let's say I have this object:
var x = [
{
name: "one",
swp: "two"
},
{
name: "two",
swp: "three"
},
{
name: "aa",
swp: "bb"
}
];
With Array.prototype.filter I might do
x.filter(function(y){ return y.name == "two"; });
However this would return only one out of the two objects that have "two" as a value in any of their properties.
Whereas
function findValue( value ) {
var y = [];
for (obj in x) {
for (val in x[obj]) {
if (x[obj][val].match( value )) {
y.push(x[obj]);
}
}
}
return y;
}
does the job, but is a brute force approach. Is there a better way to achieve the same result?