মান লজিয়ে, আমাদের নিচের মতো একটি অবজেক্টের অ্যারে আছে −
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"},
];
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এমন একটি অ্যারে নেয়। ফাংশনটির লক্ষ্য হলো মূল অ্যারে থেকে সেই সব অবজেক্ট রিটার্ন করা যাদের "from" প্রপার্টির মান "USA" এবং "to" প্রপার্টির মান "INDIA"।
উদাহরণ
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));
আউটপুট
কনসোলে আউটপুট হবে −
2