title: Find all common elements of an array of objects - Javascript
body: <p>I have data structured as follows:</p>
  
  <pre><code>var Items = [{"Name":"type1","Options":[1,2,5]},{"Name":"type2","Options":[1,2]},{"Name":"type1","Options":[2,5]}];
  </code></pre>
  
  <p>I am fairly new to javascript but I'm trying to figure out the common options associated with each name type.</p>
  
  <p>The number of elements in the Items array is arbitrary. So I could have 40 for example.</p>
  
  <p>My expected output for the above data would be </p>
  
  <pre><code>CommonOptions = [{"Name":"type1","Options":[2,5]},{"Name":"type2","Options":[1,2]}];
  </code></pre>
  
  <p>because 2 and 5 are common to all elements with name type1 and 1,2 are common to all elements with elements with name type2. I don't know how to access the data correctly.</p>
  
  <p>Here is where I am up to. If someone could steer me in the right direction I'd really appreciate it.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var Items = [{
    "Name": "type1",
    "Options": [1, 2, 5]
  }, {
    "Name": "type2",
    "Options": [1, 2]
  }, {
    "Name": "type1",
    "Options": [2, 5]
  }];
  //Define the Common Options Array as the first options and associated name
  var CommonOptions = [];
  CommonOptions.push(Items[0]);
  
  //the first item is already in the common options array so start at 1
  for (var i = 1, iLen = Items.length - 1; i &lt; iLen; i++) {
    for (var j = 0, cLen = CommonOptions.length; j &lt; cLen; j++) {
  
      //add all unique by name options to common options array
      if (CommonOptions[j].Name.indexOf(Items[i].Name) === -1) {
        //item name is not in the common options array
        //add item to common options array
        CommonOptions.push(Items[i]);
      } else {
        //item name is in the common options array
        // if it is in the common options array then check each Option in the Option array against the common options of that name
        //CommonOptions[j].Options.indexOf(Items[i].Options)===-1
      }
    }
  }
  console.log(CommonOptions);</code></pre>
  </div>
  </div>
  </p>