Diskusi Azril Syamin
Selesai
![Azril Syamin](https://image.web.id/images/FotoJet.md.jpg)
Azril Syamin
3 bulan yang lalu
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](https://image.web.id/images/avatar2.jpg)
Muhammad Fakhry Burhanuddin
3 bulan yang lalu
Jawaban Terpilih
Silahkan lihat penjelasannya di https://dashboard.codepolitan.com/learn/discussions/thread/boolean-MlC5