运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Maps</h1> <h2>The Map.groupBy() Method</h2> <p id="demo"></p> <script> // Create an Array const fruits = [ {name:"apples", quantity:300}, {name:"bananas", quantity:500}, {name:"oranges", quantity:200}, {name:"kiwi", quantity:150} ]; // Callback function to select low volumes function myCallback({ quantity }) { return quantity > 200 ? "ok" : "low"; } // Group by ok and low const result = Map.groupBy(fruits, myCallback); // Display Results let text ="These fruits are Ok: <br>"; for (let x of result.get("ok")) { text += x.name + " " + x.quantity + "<br>"; } text += "<br>These fruits are low: <br>"; for (let x of result.get("low")) { text += x.name + " " + x.quantity + "<br>"; } document.getElementById("demo").innerHTML = text; console.log(result.get("ok")); </script> </body> </html>