Diskusi Azril Syamin

Selesai
Azril Syamin
Ditanyakan pada: Pengkategorian Data Array

kenapa productExist.length?

addItem: function(product) { var productIndex; var productExist = this.cart.filter(function(item, index) { if (item.product.id == Number(product.id)) { productIndex = index; return true; } else { return false; } }); if (productExist.length) { this.cart[productIndex].qty++ } else { this.cart.push({product: product, qty: 1}); } }

kalo dilihat dari codingan ini, bukankan hasil dari productExist itu sendiri sudah bernilai true atau false,

soalan saya kenapa harus pake length?

bukankah dengan begini juga bisa?

if (productExist) { this.cart[productIndex].qty++ } else { this.cart.push({product: product, qty: 1}); }

1 Jawaban

Avatar
Muhammad Fakhry Burhanuddin

3 bulan yang lalu

Jawaban Terpilih