映射匹配字符串的元素
我有一张地图
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你不能那样做。解构仅适用于具体的键,它不能变形以更改基于数据解构的键。
如果您有一组混合数据,其中仅存在一个属性,则可以解构所有属性,然后获取包含数据的属性。这样你就可以对数组中的所有项目进行操作:
或者,如果您有一个同质数组,其中数据的形状都相同,则可以创建一个将使用普通值的函数,然后您可以指定它应从哪个属性获取该值:
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:
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: