0

I wanted to fetch the Button Click value and pass it to the map method so only that part of the array is executed, I'm trying the pass the value using back tics. can you guys help me where I'm getting this wrong?

const productData = {
  ProductA: {
    CountryA: [{
        id: 'Product A',
        avilabllity: 'Amazon',
        shopUrl: 'https://'
      }
        
    ],
   CountryB: [{
        id: 'Product B',
        avilabllity: 'Amazon',
        shopUrl: 'https://'
      }
    ],
  },
}



$( ".myButton" ).click(function() {
  
  var buttonId = this.id;
  
  //How To Dynmically Get "ProductA" data from this.id on button click so when i Click on ProdctB, this.id will change.
  const mytData = productData.ProductA.CountryA.map(myId => myId.id);
  
  
 // const whatIWant = productData.`${buttonId}`.CountryA.map(myId => myId.id);
  //console.log(whatIWant)
  
  console.log(mytData);
  console.log(buttonId)
  
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<Button class="myButton" id="productA" >Product A</Button>

<Button class="myButton" id="productB" >Product B</Button>

<Button class="myButton" id="productC" >Product C</Button>
  • 1
    `productData[buttonId]...` –  Jun 08 '20 at 17:41
  • For refrence I have only Shown the Array of Product A oonly Product B is not Listed in this, but you the idea right I want to pass the clickId to the Map function so only that data renders. Ive also tried productData[buttonId] but gives me undefined. Please help – Vimal Thanikachalam Jun 08 '20 at 17:58
  • 1
    see... looking at the structure, it seems ProductA have no ID but contains CountryA with an id value of 'Product A'. Also, ProductA contains CountryB with id 'Product B'. And all of those IDs as different from the buttons IDs which are 'productA' and 'productB'. Are you sure that structure is exactly the way it was supposed to? – Thiago E S Jun 08 '20 at 17:59
  • 1
    I was telling you about the basic idea, but if you want this to work, the capitalization needs to be fixed: `id="ProductA"` –  Jun 08 '20 at 17:59
  • Thank you, for all your response guys :) productData[buttonId] Worked like Charm :) Thank you so much. Updated the Working Code. – Vimal Thanikachalam Jun 08 '20 at 18:23
  • https://codepen.io/vimaleurakaa/pen/abdvpEp – Vimal Thanikachalam Jun 08 '20 at 18:28

0 Answers0