How do I filter a javascript object when making a search pipe with angular 2?
<p>I'm trying to create a search pipe for Angular 2.
It should take a nested object and filter out the objects matching a search term.
I have a basic version working but I have run into two problems.</p>

<p>Problem one is, that I now have to hard code key names or use what seems to me like a dirty hack with <code>JSON.stringify</code>.</p>

<p>What is a more elegant way to filter the object, matching the term with any value exept a list of hardcoded ones like <code>_id</code> and <code>url</code>?</p>

<p>Problem two is, that if the string <code>term</code> contains a space, I would like to split it and filter the <code>obj</code> with both terms.
I can split it using <code>terms = term.split(' ');</code>.</p>

<p>But how do I achive filtering with multiple terms?</p>

<p>My current code:</p>

<pre><code>import {Pipe} from 'angular2/core';

@Pipe({
    name: "search"
})
export class SearchPipe{
    transform(obj: any, [term]) {
        if (obj != null &amp;&amp; term) {
            return obj.filter( el =&gt; {

                //var test = JSON.stringify(el); 
                //return test.toLowerCase().includes(term.toLowerCase()); //trows a compile error but seems to work.

                return el.name.toLowerCase().includes(term.toLowerCase()) || el.place.toLowerCase().includes(term.toLowerCase()) || el.title.toLowerCase().includes(term.toLowerCase()) ;

            });

        } else {
            return obj;
        }
    }
}
</code></pre>

<p>Expected input format:</p>

<pre><code>[
{
  "_id": "56ffbe512ba199777d51c6ae",
  "picture": "http://placehold.it/36x36",
  "name": "Melissa Reeves",
  "company": "Orbixtar",
  "place": "Greenwich, Belarus",
  "title": "voluptate est ipsum",
  "location": {
    "lat": -78.926961,
    "lng": 90.157653
  },
  "url": "http://lol.lol"
},
{
  "_id": "56ffbe5119cf66e94b3800b4",
  "picture": "http://placehold.it/36x36",
  "name": "Chelsea Lindsay",
  "company": "Squish",
  "place": "Vowinckel, Belarus",
  "title": "anim ea exercitation",
  "location": {
    "lat": 66.547582,
    "lng": 162.720388
  },
  "url": "http://lol.lol"
}
]
</code></pre>

<p>If the term is <code>"term1"</code> I would like it to return objects that contains "term1".</p>

<p>If on the otherhand term is <code>"term1 term2 term3"</code> I would like for it to return all objects that contain all terms but not necessarily in that specific order.</p>

<p>As an exaple. </p>

<p>The term <code>"Melissa"</code> should return the first object.</p>

<p>The term <code>"Melissa Belarus"</code> should also return only the first object.
Even though the second object matches one of the keys.</p>