Javascript 未捕获引用错误:shoppingCart上未定义产品

Javascript 未捕获引用错误:shoppingCart上未定义产品,javascript,arrays,Javascript,Arrays,我的问题是,在数组中搜索时,它无法识别第一个项目,即T恤,对于下一个元素,当我单击每个按钮时,它会显示上一个元素。 你能帮我吗 const products = [ { id: 1, title: 'T-Shirt', price: 98, quantity: 10 }, { id: 2, title: 'Shoes', price: 70, quantity: 17 }, { id: 3, ti

我的问题是,在数组中搜索时,它无法识别第一个项目,即T恤,对于下一个元素,当我单击每个按钮时,它会显示上一个元素。 你能帮我吗

const products = [
  {
    id: 1,
    title: 'T-Shirt',
    price: 98,
    quantity: 10
  },
  {
    id: 2,
    title: 'Shoes',
    price: 70,
    quantity: 17
  },
  {
    id: 3,
    title: 'nude women Bag',
    price: 43,
    quantity: 19
  },
  {
    id: 4,
    title: 'Perfume',
    price: 120,
    quantity: 10
  },
  {
    id: 5,
    title: 'Bag',
    price: 230,
    quantity: 5
  },
  {
    id: 6,
    title: 'blue women bag',
    price: 120,
    quantity: 12
  }
]

let addBtn = document.querySelectorAll('.cart-btn')

const shoppingCart = (myId) => {
  localStorage.setItem('myProduct', JSON.stringify(products))
  const getStorage = localStorage.getItem('myProduct')
  const finalProducts = JSON.parse(getStorage)

  let titleResult = finalProducts.filter(item => item.id === myId)

  console.log(titleResult[product].title)
}

for (let i = 0; i < products.length; i++) {
  addBtn[i].addEventListener('click', () => {
    shoppingCart(i)
  })
}
const产品=[
{
id:1,
标题:“T恤衫”,
价格:98,
数量:10
},
{
id:2,
标题:"鞋",,
价格:70,
数量:17
},
{
id:3,
标题:“裸体女人包”,
价格:43,
数量:19
},
{
id:4,
标题:“香水”,
价格:120,
数量:10
},
{
id:5,
标题:"包",,
价格:230,
数量:5
},
{
id:6,
标题:“蓝色女人包”,
价格:120,
数量:12
}
]
让addBtn=document.querySelectorAll('.cart btn')
const shoppingCart=(myId)=>{
localStorage.setItem('myProduct',JSON.stringify(产品))
const getStorage=localStorage.getItem('myProduct')
const finalProducts=JSON.parse(getStorage)
让titleResult=finalProducts.filter(item=>item.id==myId)
console.log(titleResult[product].title)
}
for(设i=0;i{
购物车(一)
})
}

您的循环从
0
开始,但您的id从1开始,您可以使用
shoppingCart(i+1)偏移
i
以匹配您的id
非常感谢您,它工作正常:)