【发布时间】:2021-08-20 10:16:43
【问题描述】:
给定数据库中的array 中的object:
detail : [
0: {
Code: "Code 1",
Price: "1.00",
IncTaxPrice: "1.20",
Tax:"0.20",
},
1: {
Code: "Code 2",
Price: "9.00",
IncTaxPrice: "9.20",
Tax:"0.20",
}
]
我想要的输出,键和值为负(例如)
[
{
Code: "Code 1",
Price: "-1.00",
IncTaxPrice: "-1.20",
Tax: "-0.20",
},
{
Code: "Code 2",
Price: "-9.00",
IncTaxPrice: "-9.20",
Tax: "-0.20",
},
];
我尝试使用map,过滤器function,但它给出了一个错误,我想我错过了一些东西。
正确过滤后,我将使用-Math.abs(),将positive 转换为negative。
let final = detail.map(a => Object.values(a).filter(v => typeof v === 'number'))
像price,tax 这样的元素类似于numbers 和amounts 中的8 个不同的elements。
【问题讨论】:
-
"出现错误" 什么错误?请注意
detail的语法无效。 -
您的数组对象中没有数值。只有字符串值。
-
如果是财务,请小心使用
Math.abs(),只需使用var newvalue = -oldvalue翻转标志即可。这是因为理论上价格可能是负数,也就是退款。 -
@Andreas 另外
typof将为每个返回"string",因为它们都不是数字。 -
filter(v => !isNaN(Number(v))你应该改变你的过滤器,因为所有的数字都是一个字符串。
标签: javascript arrays converters negative-number arrayobject