Among other things, this question seems like a simple misunderstanding of prices.
The unit cost of the F-35A is $80M, while a top-end Gripen costs a fairly similar $60M. When Denmark did look at buying the Gripen, it requested upgrades to power and payload (already satisfied by the F-35) that could easily have made up the remaining price gap. Maintenance and flight costs are substantially higher for the F-35 also, but that's relatively manageable for a country that doesn't expect regular deployments and a large supply of spares for their fleet.
It's the development cost where the F-35 comes in at more than 10x the price of the Gripen, which is largely irrelevant to small buyers ordering a finished product. Denmark did end up contributing to the F-35 development costs, but only about the cost of another plane, $110M. And in return, they stand to compete for parts-development contracts which wouldn't have been available for the Gripen.