1

I'm trying to filter an array of javascript objects by a string. But I want the filter to look at every property and test it to see if the string is valid. AngularJS has a built in filter that does this, but I can't find any solutions for it on SO.

[
    {
        "title":"Mr.",
        "name":"John Smith",
        "firstName":"John",
        "lastName":"Smith"
    },
    {
        "title":"Mr.",
        "name":"Bill Smith",
        "firstName":"Bill",
        "lastName":"SMith"
    }

]

So for example if I entered 'Jo' for the text string it would bring back the object at index 0, which is pretty easy to do if you just want to search by a single property.

Now if I enter 'Mr' it should bring back both items at index 0 and index 1 because we're searching all of the properties.

Hope this makes sense as to what I'm asking.

EDIT: Very sorry was a late night last night and I left off very significants details in the data structure.

{
        "title":"Mr.",
        "name":"John Smith",
        "firstName":"John",
        "lastName":"Smith",
        "contactType":{
            "name":"test"
        },
        "addresses":[
            {"address":"Test Street One"},
            {"address":"Test Street Two"},
        ]
    },
    {
        "title":"Mr.",
        "name":"Bill Smith",
        "firstName":"Bill",
        "lastName":"SMith",
        "contactType":{
            "name":"test"
        },
        "addresses":[
            {"address":"Test Street One"},
            {"address":"Test Street Two"},
        ]
    }

So in this scenario the search would account for any type and any number of nested objects within objects. Sorry for forgetting this part.

1
  • "I'm trying to...", show us what you tried. Commented Jan 27, 2018 at 5:50

3 Answers 3

1

You can loop through the objects of the array and use indexof to find the elements which has the input string,

DEMO

var myarray =[
    {
        "title":"Mr.",
        "name":"John Smith",
        "firstName":"John",
        "lastName":"Smith"
    },
    {
        "title":"Mr.",
        "name":"Bill Smith",
        "firstName":"Bill",
        "lastName":"SMith"
    }

];
var toSearch = "Mr";
var results =[];
for(var i=0; i<myarray.length; i++) {
  for(key in myarray[i]) {
    if(myarray[i][key].indexOf(toSearch)!=-1) {
      results.push(myarray[i]);
    }
  }
  }
  
  console.log(results)

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a reason you don't use the filter function of arrays?
no you can still use filter, just that i explained the logic
1

You can use the combination of Array#filter and Object.values in order to achieve this:

let data = [{
    "title": "Mr.",
    "name": "John Smith",
    "firstName": "John",
    "lastName": "Smith"
  },
  {
    "title": "Mr.",
    "name": "Bill Smith",
    "firstName": "Bill",
    "lastName": "SMith"
  }

];

let string = 'Jo';
let results = data.filter(item => Object.values(item).some(value => value.includes(string)));

console.log(results);

1 Comment

You could shorten it to let results = data.filter(item => Object.values(item).some(value => value.includes(string))); since that will be automatically returned.
0

Loop through all properties for each object :

var items = [{
    "title": "Mr.",
    "name": "John Smith",
    "firstName": "John",
    "lastName": "Smith"
}, {
    "title": "Mr.",
    "name": "Bill Smith",
    "firstName": "Bill",
    "lastName": "SMith"
}];

console.log("Jo :", items.filter(x => contains(x, "Jo")).map(x => x.name));
console.log("Mr :", items.filter(x => contains(x, "Mr")).map(x => x.name));

function contains (x, w) {
  for (let k in x) {
    if (x[k].indexOf(w) !== -1) {
      return true;
    }
  }
  return false;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.