Cho mảng đối tượng, cần đếm số đối tượng có from = "USA" và to = "INDIA".
Ví dụ
const arr = [ {"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ]; const findDesiredLength = (arr = [], from = 'USA', to = 'INDIA') => { const filtered = arr.filter(el => { if(el.from === from && el.to === to){ return true; } }); const { length: l } = filtered || []; return l; }; console.log(findDesiredLength(arr));
Kết quả
2