使用.gin。在JavaScript中操纵一系列对象的问题

发布于 2025-01-24 22:57:19 字数 2081 浏览 5 评论 0原文

我一直试图解决此问题一段时间,但无法弄清楚。 我从API中收到了一些产品,在解构后,我将它们传递到了一些HTML标签中,从JS这样的JS对其进行操作...

    displayProducts(products){
        let result = '';
        products.forEach(product => {
            result += `
            <article class="product">
                <div class="img-container">
                    <img src=${product.image} alt="product" class="product-img">
                    <button class="bag-btn" data-id= ${product.id}>
                         <img src="./images/icons8_add_shopping_cart_100px.png" 
                         width="16px" max-width= "18px" alt="add to cart"/>
                         Add to cart
                    </button>
                </div>
                <div class="goodsinfo">
                    <span class="description"> <img src="./images/icons8_eye_100px.png" class="view" data-class=${product.id}/> 
                    </span>
                    <span class="titleprice">
                        <h3>${product.title}</h3>
                        <h4>#${product.price}</h4>
                    </span>
                </div>
            </article>
            `
        });
        productsDOM.innerHTML = result;

    }

然后我开始操纵,但是我使用.find rand array方法遇到了一个问题,以匹配。具有来自对象数组的ID,即使它们完全相同。

 compileDescription(products){
        console.log(products) // this works fine and displays 16 products which are 16 objects in arrays.

        const eyeView = [...document.querySelectorAll('.view')];
        
        eyeView.forEach(viewBtn=>{
        viewBtn.addEventListener('click', (event)=>{
        const btnId = event.target.dataset.class
        console.log(btnId); //this works and shows the ID of the clicked button

        const productFind = products.find(product=> product.id === btnId) 

        console.log(productFind); //returns undefined
        }) 
        })
   
    }

我想要的是,当单击具有特定ID的按钮时,ID与对象数组中的对象的ID匹配,并且对象将返回给我进行操作。 请帮助我。谢谢。

I have been trying to get this solved for a while, but can't figure it out.
I received some products from an API and after deconstructing, I passed them into some HTML tags to perform DOM manipulation on them from JS like so...

    displayProducts(products){
        let result = '';
        products.forEach(product => {
            result += `
            <article class="product">
                <div class="img-container">
                    <img src=${product.image} alt="product" class="product-img">
                    <button class="bag-btn" data-id= ${product.id}>
                         <img src="./images/icons8_add_shopping_cart_100px.png" 
                         width="16px" max-width= "18px" alt="add to cart"/>
                         Add to cart
                    </button>
                </div>
                <div class="goodsinfo">
                    <span class="description"> <img src="./images/icons8_eye_100px.png" class="view" data-class=${product.id}/> 
                    </span>
                    <span class="titleprice">
                        <h3>${product.title}</h3>
                        <h4>#${product.price}</h4>
                    </span>
                </div>
            </article>
            `
        });
        productsDOM.innerHTML = result;

    }

Then I started manipulating, but I ran into a problem using the .find array method to match an id with the id from the array of objects even though they're exactly the same.

 compileDescription(products){
        console.log(products) // this works fine and displays 16 products which are 16 objects in arrays.

        const eyeView = [...document.querySelectorAll('.view')];
        
        eyeView.forEach(viewBtn=>{
        viewBtn.addEventListener('click', (event)=>{
        const btnId = event.target.dataset.class
        console.log(btnId); //this works and shows the ID of the clicked button

        const productFind = products.find(product=> product.id === btnId) 

        console.log(productFind); //returns undefined
        }) 
        })
   
    }

What I want is that when a button with a particular id is clicked, the id is matched with an id of an object in the array of objects and the object is returned to me for manipulation.
Please help me guys. Thanks ahead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文