title: How to extract certain elements from an array?
body: <p>The below code accesses and array with objects in it. Each object has and id, an url and a Tag. I want to be able to basically use the Tag to display a random URL connected with a tag I suggest.</p>
  
  <p>For example i want window.currentlyShownOffer.Tag to always be equal to Garden. Garden is a Tag.</p>
  
  <pre><code>function receivedData(data) {
  
          function changeOfferPicture() {
              if (window.currentlyShownOffer) {
                  var tagArray = data[window.currentlyShownOffer.Tag];
                  tagArray.splice(tagArray.indexOf(currentlyShownOffer), 1);
                  //alert(window.currentlyShownOffer.ImagesId);
  
              }
  
  
              var tags = Object.keys(data);
              var randomTag = tags[Math.floor(Math.random() * tags.length)];
              var tagArray = data[randomTag];
  
  
              window.currentlyShownOffer = tagArray[Math.floor(Math.random() * tagArray.length)];
              document.getElementById('content').src = window.currentlyShownOffer.ImagesPath;
          };
  }
  </code></pre>
  
  <p>Any idea of what the array looks like:</p>
  
  <pre><code>{“Garden”:[{“ImagesId”:”63”,”ImagesPath”:”http….”,”Tag”:”Garden”},{“Garden”:[{“ImagesId”:”64”,”ImagesPath”:”http….”,”Tag”:”Garden”}]{“Food”:[{“ImagesId”:”63”,”ImagesPath”:”http….”,”Tag”:”Food”},{“Food”:[{“ImagesId”:”63”,”ImagesPath”:”http….”,”Tag”:”Food”}]
  </code></pre>
  
  <p>UPDATE</p>
  
  <pre><code>function receivedData(data) {
  
          function changeOfferPicture() {
              if (window.currentlyShownOffer) {
                  var tagArray = data[window.currentlyShownOffer.Tag];
                  tagArray.splice(tagArray.indexOf(currentlyShownOffer), 1);
                  //alert(window.currentlyShownOffer.ImagesId);
  
              }
  
  
              var tags = Object.keys(data);
              var randomTag = tags[Math.floor(Math.random() * tags.length)];
              var tagArray = data[randomTag];
  
           function getObjectsWithTag(tag, tagArray){
                  return tagArray.filter(function(item){
                  return item.tag === tag;
              });
              }
  
              getObjectsWithTag(Garden);
  
  
              window.currentlyShownOffer = tagArray[Math.floor(Math.random() * tagArray.length)];
              document.getElementById('content').src = window.currentlyShownOffer.ImagesPath;
          };
  }
  </code></pre>
  
  <p>Here's maybe a better idea of the array:</p>
  
  <pre><code>{
  “Garden’ : [{‘ImagesId”: ”38”, ”ImagesPath”: ”url”, ”Tag”: ”Food”}],
  “Sport’: [{‘ImagesId”: ”64”, ”ImagesPath”: ”url”, ”Tag”: ”Sport”}]
  }
  </code></pre>