我如何使用for循环console.log shoppingCart数组中对象的name属性

时间:2018-10-22 17:28:10

标签: javascript arrays for-loop

var shoppingCart = [{
  id: 0,
  name: 'Mens Shirt',
  price: 20,
  size: 'Large'
}, {
  id: 1,
  name: 'kids shirt',
  price: 15,
  size: 'small'
}]

1 个答案:

答案 0 :(得分:0)

您可以像这样简单地使用Array.prototype.forEach

var shoppingCart = [{
  id: 0,
  name: 'Mens Shirt',
  price: 20,
  size: 'Large'
}, {
  id: 1,
  name: 'kids shirt',
  price: 15,
  size: 'small'
}]

shoppingCart.forEach(item => console.log(item.name));