0

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.

For example i want window.currentlyShownOffer.Tag to always be equal to Garden. Garden is a Tag.

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;
        };
}

Any idea of what the array looks like:

{“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”}]

UPDATE

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;
        };
}

Here's maybe a better idea of the array:

{
“Garden’ : [{‘ImagesId”: ”38”, ”ImagesPath”: ”url”, ”Tag”: ”Food”}],
“Sport’: [{‘ImagesId”: ”64”, ”ImagesPath”: ”url”, ”Tag”: ”Sport”}]
}
4
  • You can access/find items in the array using indexOf() w3schools.com/jsref/jsref_indexof.asp Commented Oct 2, 2014 at 0:16
  • okay, so what's not working in this code that you think should work? I'm not seeing an actual question, you're more hinting that "something" is wrong, which isn't quite enough information Commented Oct 2, 2014 at 0:22
  • This piece of code is giving me a random url from all the Tags. I want to just get a random url from just the ones that have Tag: Garden Commented Oct 2, 2014 at 0:27
  • updated my code to the most recent example Commented Oct 2, 2014 at 1:05

1 Answer 1

1

You can use Array.filter() to get all items with a specific tag. If tagArray has Objects with a key tag containing the tag name:

function getObjectsWithTag(tag, tagArray){
  return tagArray.filter(function(item){
    return item.tag === tag;
  });
}

The returned value will be an array containing only items that matched the tag name in the original tagArray.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I 'm new to this and I'm struggling to implement what you are suggesting.. I added an update to my code above with what I have.
Take a look at your sample array again. There are mismatched brackets, which make it difficult to understand...
I added maybe a better idea of the array under the updated code
Re-do the whole question, presenting a good example of your data and what needs to be extracted from your data, without your the code. Right now, it's just getting more confusing and too specific to your application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.