<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>The Object.groupBy() Method</h2>
<p id="demo"></p>
<script>
const fruits = [
{name:"apples", quantity:300},
{name:"bananas", quantity:500},
{name:"oranges", quantity:200},
{name:"kiwi", quantity:150}
];
function myCallback({ quantity }) {
return quantity > 200 ? "ok" : "low";
}
const result = Object.groupBy(fruits, myCallback);
let text ="These fruits are Ok: <br>";
for (let [x,y] of result.ok.entries()) {
text += y.name + " " + y.quantity + "<br>";
}
text += "<br>These fruits are low: <br>";
for (let [x,y] of result.low.entries()) {
text += y.name + " " + y.quantity + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>