/    Sign up×
Community /Pin to ProfileBookmark

A JAVASCRIPT ARRAY PROBLEM FROM AN API CALL

I’m currently building an ecommerce website using HTMl, CSS, Javascript and Contentful as a serverless backend and database. I make Api calls for my products and then manipulate the product title, description, image, id and price that Contentful delivers. I already created the right classes, manipulated the data and displayed the products. Here is where I have been stuck for the past 6 weeks. I want a functionality where when I click an eye icon next to a product, it displays a fullscreen modal of that product that includes a description, an image, price and add to cart button. We’ve all seen it on Ecommerce sites;
The logic goes thus:

  • Pass all the IDs of the products from the api to the eye icons as data-ids ( already did this)

  • Assemble all eye icons into a Node List in my JS file (already did this)

  • Use a ForEach statement to loop through the nodelist (already did this)

  • Add an Event Listener to the forEach argument name and bind it to a click event (already did this)

  • Create a variable “btnId”, and assign the ID of the specific eye icon that was clicked to this variable (already did this)

  • Find the product in the array of products received via the API that has the same ID as the button that was clicked and assign it to a new variable “productItem”(THIS IS WHERE MY PROBLEM IS);

  • Call a function that displays the description modal and pass in the variable “productItem” above as argument.
    I have tried everything but I can’t access the product that has the ID that is equals to “btnId”.
  • compileDescription(products){
    const eyeView = […document.querySelectorAll(‘.view’)];
    eyeView.forEach(viewBtn => {
    viewBtn.addEventListener(‘click’, (event)=>{
    const btnId= event.target.dataset.class;
    console.log(btnId);
    let productItem = product.find(product => product.id === btnId)
    if (productItem) {
    this.displayDescription(productItem);
    };
    })
    })
    }

    product.id here is undefined because product is the name of an array of objects.
    How do i access a specific product from an array of object without knowing which array index i will be accessing ahead of time?
    Please help!! I’ve been on this for over 6 weeks now. Thanks in advance

    Here is a link to the website: https://chayyahsmart.netlify.app
    Here is a link to my full code: https://codepen.io/omotovh/pen/JjWaXKo

    to post a comment

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @daveyerwinJun 10.2021 — @omotovh#1632756

    what is the error you encounter ?

    you say "product is the name of an array of objects"

    where does product come from ?

    how do you call the function compileDescription(products) ?

    compileDescription('what do you pass to function here')

    the parameter 'products' is not referenced in the code
    Copy linkTweet thisAlerts:
    @omotovhauthorJul 09.2021 — @DaveyErwin#1632785 products is received via an API call to contentful. Contentful is a serverless content management system. I'm sorry i'm just replying. I sort of abandoned the project. I just came back to it.
    ×

    Success!

    Help @omotovh spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.23,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...