How do I grab all the unique values in a fetch request?

Viewed 94

async function fetchTemplates() {
    let seturl = `https://wax.api.atomicassets.io/atomicassets/v1/templates?collection_name=mlb.topps&schema_name=series1}&has_assets=true&page=1&limit=3414&order=desc&sort=created`;
    
    try {
        let res = await fetch(seturl);
        return await res.json();
    } catch (error) {
        console.log(error);
    }

}

async function renderFilterVariants(arg) {
    let sets = await fetchTemplates();
    console.log(sets);

    if( !Array.isArray( sets.data ) )
    {
        throw new Error( "atts.data is not an array." );
    }

    const team  = sets.data.map( d => d.immutable_data.${arg});
    console.log(team);
    
}

renderFilterVariants(cardid);

Why doesn't this work? I want the function to be able to pass a custom variation under d.immutable_data

enter image description here

It has to be dynamic as the variations under immutable_data is different depending on page.

enter image description here

1 Answers

The problem is with your line const team = sets.data.map( d => d.immutable_data.${arg}); In order to get into an object key using a parameter (like arg) you shouldn't use a dot (in your code - .${arg}) but with brackets [ ] .

Example

Say you have an object foo. foo = { bar: 5} In order to get the key 'bar' inside 'foo' you can do both: foo.bar or foo['bar']

Meaning

In you code you should write :
const team = sets.data.map( d => d.immutable_data[arg]);

again this cause since arg (your variable) is a string, and when you using a string to get key in javascript object you should use brackets.

Related