Getting and using values from selected Object.keys

I have an array as below.

var Brand = { "Brand1-Parameter1-SubBrand1": [{ "Image": "animal", "Heading": "SubBrand1", "Link": "SubBrand1" }], 
                "Brand1-Parameter1-SubBrand2": [{ "Image": "animal", "Heading": "SubBrand2", "Link": "SubBrand2" }], 
                "Brand1-Parameter2-SubBrand3": [{ "Image": "water", "Heading": "SubBrand3", "Link": "SubBrand3" }], 
                "Brand1-Parameter2-SubBrand4": [{ "Image": "water", "Heading": "SubBrand4", "Link": "SubBrand4" }], 
                "Brand2-Parameter1-SubBrand5": [{ "Image": "travel", "Heading": "SubBrand5", "Link": "SubBrand5" }], 
                "Brand2-Parameter1-SubBrand6": [{ "Image": "travel", "Heading": "SubBrand6", "Link": "SubBrand6" }], 
                "Brand2-Parameter2-SubBrand7": [{ "Image": "flower", "Heading": "SubBrand7", "Link": "SubBrand7" }], 
                "Brand2-Parameter2-SubBrand8": [{ "Image": "flower", "Heading": "SubBrand8", "Link": "SubBrand8" }], 
            }

A small filter function gives me the two Object.keys from the above array:
var ParentBrandParameterKeys = Object.keys(Brand).filter(v => v.split("-").includes(ParentBrandSelected) && v.split("-").includes(ParameterSelected))

console.log(ParentBrandParameterKeys) gives me the below:

["Brand1-Parameter1-SubBrand1", "Brand1-Parameter1-SubBrand2"]

What I need is to use the values of the above selected keys to build a small div structure as below:

jQuery.each(ParentBrandParameterKeys, function(index, value){
        markup += '<a href="/Directory/SubDirectory/'+ Brand[value].Link +'.html">'
        markup += '<div class="InnerBlock">'
        markup += '<div style="background-image:url(https://source.unsplash.com/1280x720/?' + Brand[value].Image +')">'+ Brand[value].Heading +'</div>'
        markup += '</div>'
        markup += '</a>'
     });

But this isn’t working. How do I get the values for the selected Object.keys?

You don’t actually need to split; string.includes works just as well in this situation, because you dont care which of the items contained the phrase; unless you’re concerned that the search pattern is contained as a substring of a part of the string?

What you have is close.

var Brand = { "Brand1-Parameter1-SubBrand1": [{ "Image": "animal", "Heading": "SubBrand1", "Link": "SubBrand1" }], 

Note that the value of the key is an array []. It’s an array with a single value that is an object, but it’s still an array, so you’ll need to access it as such.

So to access “animal”, it would be Brand.Brand1-Parameter1-SubBrand1[0].Image
or Brand[value][0].Image, in the variable access.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.