title: How to get values in an array of objects using another array - Javascript
body: <p>I have a function that interacts with 2 arrays, 1st array is an array of objects that contain my dropdown options, second array is an array of values. I'm trying to filter the 1st array to return what has matched the values in my 2nd array. How do I achieve this?</p>
  <p>1st Array:</p>
  <pre><code>const books = [
      {
          label: &quot;To Kill a Mockingbird&quot;,
          value: 1
      },
      {
          label: &quot;1984&quot;,
          value: 2
      },
      {
          label: &quot;The Lord of the Rings&quot;,
          value: 3
      },
      {
          label: &quot;The Great Gatsby&quot;,
          value: 4
      }
  ]
  </code></pre>
  <p>Code Snippet below:</p>
  <pre><code>const idString = &quot;1,2,3&quot;;
  
  function getSelectedOption(idString, books) {
          const ids = idString.split(&quot;,&quot;);
          const selectedOptions = [];
  
          ids.map(e =&gt; {
              const selected = books.map(options =&gt; {
                  if (options.value === e){
                      return {
                          label: options.label,
                          value: options.value
                      }
                  }
              })
              selectedOptions.push(selected)
          })
  
          return selectedOptions
      }
  </code></pre>
  <p>Result:</p>
  <pre><code>[
      [undefined, undefined, undefined, undefined],
      [undefined, undefined, undefined, undefined],
      [undefined, undefined, undefined, undefined]
  ]
  </code></pre>
  <p>Expected Result:</p>
  <pre><code>[
      {
          label: &quot;To Kill a Mockingbird&quot;,
          value: 1
      },
      {
          label: &quot;1984&quot;,
          value: 2
      },
      {
          label: &quot;The Lord of the Rings&quot;,
          value: 3
      }
  ]
  </code></pre>