title: Reducing object array with fallback values
body: <p>Consider the following object:</p>
  <pre><code>var content = [
      {
          id: 'tab1',
          langCode: 'en',
          title: 'rocket'
      },
      {
          id: 'tab1',
          langCode: 'da',
          title: 'raket'
      },
      {
          id: 'tab2',
          langCode: 'en',
          title: 'factory'        
      },
      {
          id: 'tab3',
          langCode: 'es',
          title: 'boligrafo'
      },      
  ];
  </code></pre>
  <p>I'd like to reduce this array, to a new array with the following restriction:</p>
  <ul>
  <li>No duplicate IDs</li>
  <li>Values from the local language should take preference</li>
  <li>In case there are no local translation, fall back to English</li>
  <li>All other translations should be discarded (even if they have a unique ID)</li>
  </ul>
  <p>That means, the output from the data above would be as follows, if the local language were Danish:</p>
  <pre><code>[
      {
          id: 'tab1',
          langCode: 'da',
          title: 'raket'
      },
      {
          id: 'tab2',
          langCode: 'en',
          title: 'factory'        
      },
  ];
  </code></pre>
  <p>My goal is to keep the code as short and readable as possible, and I have ES6 and Lodash at my full disposal. This is what I have so far:</p>
  <pre><code>const LOCAL_LANG = 'da'; // Hard coded for the sake of the example
  
  let localArr = content.filter(item =&gt; item.langCode === LOCAL_LANG);
  
  if(LOCAL_LANG !== 'en') {
      let enArr = content.filter(item =&gt; item.langCode === 'en');
      for (let i = 0; i &lt; enArr.length; i++) {
          if (!_.find(localArr, { 'id': enArr[i].id})) {
              localArr.push(enArr[i]);
          }
      }
  }
  </code></pre>
  <p>This does the trick, but it creates two duplicate arrays, and then merges them back together in what I feel is a clumpsy way. I would like to see a more elegant solution – preferably one, where I don't have to pass over the arrays so many times.</p>
  <p>An alternative (and perhaps slightly cleaner) solution would be to reduce the array on <code>langCode === 'da' || langCode === 'en'</code> on first pass, and then remove duplicates ... but still I'm just feeling I'm missing the most obvious solution.</p>
  <p>Thanks!</p>