Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should do it:</p> <pre><code>var validTags = [{"tag":"tag1"}, {"tag":"tag2"}]; var items = [{"id":123456, "tag":"tag1"}, {"id":123456, "tag":"tag2"}, {"id":7890, "tag":"tag1"}]; var actualTags = validTags.map(function(obj){return obj.tag}), comparableTags = actualTags.sort().join(" "); var tagsById = items.reduce(function(map, item) { if (item.id in map) map[item.id].push(item.tag); else map[item.id] = [ item.tag ]; return map; }, {}); var result = []; for (var id in tagsById) { var tags = tagsById[id].sort().join(" "); if (comparableTags == tags) // Yai, array comparison by content! result.push({id: id, tag:tags}); } return result; </code></pre> <p>If you were using <a href="http://underscorejs.org/" rel="nofollow">Underscore</a>, you could use <a href="http://underscorejs.org/#pluck" rel="nofollow"><code>pluck</code></a> instead of the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Map" rel="nofollow"><code>map</code></a> and <a href="http://underscorejs.org/#groupBy" rel="nofollow"><code>groupBy</code></a> instead of the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce" rel="nofollow"><code>reduce</code></a>; in short:</p> <pre><code>var comparableTags = _.pluck(validTags, "tag").sort().join(" "); return _.chain(items).groupBy("id").map(function(id, tags) { return {id:id, tag:tags.sort().join(" "); }.filter(function(obj) { return obj.tag == comparableTags; }).value(); </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. This table or related slice is empty.
    1. 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