映射匹配字符串的元素

发布于 2025-01-09 10:02:38 字数 308 浏览 0 评论 0原文

我有一张地图

.map((item: { amount: number}) =>

问题是 object prop 并不总是 amount 但会以此开始。所以它可能是

。 map((item: { amountTotal: number}) =>

.map((item: { amountConsolidated: number}) =>

有没有办法正则表达式或部分匹配字符串,以便地图函数可以覆盖所有对象名称?

I have a map

.map((item: { amount: number}) =>

The issue is that object prop is not always amount but will start with that. so it could be

.map((item: { amountTotal: number}) =>

.map((item: { amountConsolidated: number}) =>

Is there a way to regex or partially match the string so the map function can cover all the object names?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-16 10:02:38

不,你不能那样做。解构仅适用于具体的键,它不能变形以更改基于数据解构的键。

如果您有一组混合数据,其中仅存在一个属性,则可以解构所有属性,然后获取包含数据的属性。这样你就可以对数组中的所有项目进行操作:

const data = [
  {amount:             1}, 
  {amountTotal:        2},
  {amountConsolidated: 3},
];

const result = data.map(({amount, amountTotal, amountConsolidated}) => {
  //pick the first one that exists
  const num = amount ?? amountTotal ?? amountConsolidated;
  
  //example mapping operation
  return num * 2;
});

console.log(result);


或者,如果您有一个同质数组,其中数据的形状都相同,则可以创建一个将使用普通值的函数,然后您可以指定它应从哪个属性获取该值:

const data1 = [
  {amount: 1}, 
  {amount: 1},
  {amount: 1},
];

const data2 = [
  {amountTotal: 2}, 
  {amountTotal: 2},
  {amountTotal: 2},
];

const data3 = [
  {amountConsolidated: 3}, 
  {amountConsolidated: 3},
  {amountConsolidated: 3},
];

//curried mapper that will apply a function against a given property
const mapper = fn => prop => item =>
  fn(item[prop]);

//use a function that works with plain numbers
const someMappingOperation = mapper(num => num * 2);

//specify which property the number comes from depending on the array
const result1 = data1.map(someMappingOperation("amount"));
const result2 = data2.map(someMappingOperation("amountTotal"));
const result3 = data3.map(someMappingOperation("amountConsolidated"));

console.log(result1);
console.log(result2);
console.log(result3);

No, you cannot do that. Destructuring only works with concrete keys, it cannot morph to change the key destructured based on the data.

If you have an array of mixed data where only one of the properties would be present, you can destructure all and then get the one with data. That way you can operate on all the items in the array:

const data = [
  {amount:             1}, 
  {amountTotal:        2},
  {amountConsolidated: 3},
];

const result = data.map(({amount, amountTotal, amountConsolidated}) => {
  //pick the first one that exists
  const num = amount ?? amountTotal ?? amountConsolidated;
  
  //example mapping operation
  return num * 2;
});

console.log(result);


Alternatively, if you have a homogenous array where the data is all the same shape, you can create a function that will work with a plain value and then you can specify which property it should take it from:

const data1 = [
  {amount: 1}, 
  {amount: 1},
  {amount: 1},
];

const data2 = [
  {amountTotal: 2}, 
  {amountTotal: 2},
  {amountTotal: 2},
];

const data3 = [
  {amountConsolidated: 3}, 
  {amountConsolidated: 3},
  {amountConsolidated: 3},
];

//curried mapper that will apply a function against a given property
const mapper = fn => prop => item =>
  fn(item[prop]);

//use a function that works with plain numbers
const someMappingOperation = mapper(num => num * 2);

//specify which property the number comes from depending on the array
const result1 = data1.map(someMappingOperation("amount"));
const result2 = data2.map(someMappingOperation("amountTotal"));
const result3 = data3.map(someMappingOperation("amountConsolidated"));

console.log(result1);
console.log(result2);
console.log(result3);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文