从 lodash/fp 的国家列表中获取货币
Get currencies from countries list with lodash/fp
如何使用 Lodash 从这组国家/地区中获取每个 currencySimbol
的一组货币?
我曾经有这个 getCurrencies 函数,但我无法用它获取符号,所以现在我做了这个 getCurrenciesWithSymbol
但它正在制作重复项和类似这样的东西 {key: "", displayName: "", currencySymbol: undefined}
import { flow, map, uniq, keyBy, compact, pick } from "lodash/fp";
const countries = [
{
key: "US",
name: "United States",
currency: "USD",
currencySymbol: "$"
},
{
key: "TW",
name: "Taiwan",
currency: "TWD",
currencySymbol: ""
}
];
function getCurrencies(countries) {
const currencies = flow(
map("currency"),
uniq,
compact,
map((each) => ({ key: each, displayName: each }))
)(countries);
return {
asList: currencies,
byKey: keyBy("key", currencies)
};
} // [{key: "USD", displayName: "USD"}, {key: "TW", displayName: "TW"}]
function getCurrenciesWithSymbol(countries) {
const currencies = flow(
map(pick(["currency", "currencySymbol"])),
uniq,
compact,
map((each) => ({
key: each.currency,
displayName: each.currency,
currencySymbol: each.currencySymbol
}))
)(countries);
return {
asList: currencies,
byKey: keyBy("key", currencies)
};
}
//Expected
// [
// { displayName: "USD", key: "USD", currencySymbol: "$" },
// { displayName: "TW", key: "TW", currencySymbol: "" }
// ];
使用_.uniqBy('currency')
对数组进行去重,然后映射到要求的形式:
const { flow, uniqBy, map } = _;
const getCurrenciesWithSymbol = flow(
uniqBy('currency'),
map(({ currency, currencySymbol }) => ({
displayName: currency,
key: currency,
currencySymbol
}))
);
const countries = [{"key":"US","name":"United States","currency":"USD","currencySymbol":"$"},{"key":"TW","name":"Taiwan","currency":"TWD","currencySymbol":""}];
const result = getCurrenciesWithSymbol(countries);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
如何使用 Lodash 从这组国家/地区中获取每个 currencySimbol
的一组货币?
我曾经有这个 getCurrencies 函数,但我无法用它获取符号,所以现在我做了这个 getCurrenciesWithSymbol
但它正在制作重复项和类似这样的东西 {key: "", displayName: "", currencySymbol: undefined}
import { flow, map, uniq, keyBy, compact, pick } from "lodash/fp";
const countries = [
{
key: "US",
name: "United States",
currency: "USD",
currencySymbol: "$"
},
{
key: "TW",
name: "Taiwan",
currency: "TWD",
currencySymbol: ""
}
];
function getCurrencies(countries) {
const currencies = flow(
map("currency"),
uniq,
compact,
map((each) => ({ key: each, displayName: each }))
)(countries);
return {
asList: currencies,
byKey: keyBy("key", currencies)
};
} // [{key: "USD", displayName: "USD"}, {key: "TW", displayName: "TW"}]
function getCurrenciesWithSymbol(countries) {
const currencies = flow(
map(pick(["currency", "currencySymbol"])),
uniq,
compact,
map((each) => ({
key: each.currency,
displayName: each.currency,
currencySymbol: each.currencySymbol
}))
)(countries);
return {
asList: currencies,
byKey: keyBy("key", currencies)
};
}
//Expected
// [
// { displayName: "USD", key: "USD", currencySymbol: "$" },
// { displayName: "TW", key: "TW", currencySymbol: "" }
// ];
使用_.uniqBy('currency')
对数组进行去重,然后映射到要求的形式:
const { flow, uniqBy, map } = _;
const getCurrenciesWithSymbol = flow(
uniqBy('currency'),
map(({ currency, currencySymbol }) => ({
displayName: currency,
key: currency,
currencySymbol
}))
);
const countries = [{"key":"US","name":"United States","currency":"USD","currencySymbol":"$"},{"key":"TW","name":"Taiwan","currency":"TWD","currencySymbol":""}];
const result = getCurrenciesWithSymbol(countries);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>