Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use something like this to compare two Javascript objects:</p> <pre><code>// returns true if "obj" contains "other" as a subset contains = function(obj, other) { return Object.keys(other).every(function(key) { return other[key] == obj[key]; }); } </code></pre> <p>for example, <code>contains(users[0], {profile: 'student'} )</code> returns <code>true</code>.</p> <p>Once we have this, the rest is easy:</p> <pre><code> userFilter = { ...criteria... } results = users.filter(function(item) { return contains(item, userFilter) }) </code></pre> <p>Note that this does <code>AND</code> matching, that is, if the filter is <code>{state:5, profile:'student'}</code>, it finds records that have both state=5 and profile=student. To do <code>OR</code> matching instead, replace <code>every()</code> with <code>some()</code> in the above code.</p> <p>As per your comment, the above function can be generalized by adding a comparison function as a parameter:</p> <pre><code>contains = function(obj, other, compare) { return Object.keys(other).every(function(key) { return compare ? compare(key, obj[key], other[key]) : obj[key] == other[key]; }); } </code></pre> <p>Comparison function accepts key, object value and filter value and is supposed to return true or false. Example:</p> <pre><code>user = { "id": "52", "name": "Foo Bar" } // this uses the default "equals" comparison and fails contains(user, { name: "Foo" }); // this uses a custom "indexOf" comparison and succeeds contains(user, { name: "Foo" }, function(key, val, flt) { return val.indexOf(flt) &gt;= 0; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload