更新所有对象值以保留两位小数
Update all object values to be rounded by two decimals
我有一个来自 API 的动态响应,它每次都有不同的响应。我需要遍历一个对象数组并找到数字类型的值。然后将它们四舍五入到小数点后两位。在下面的示例中,对象键值每次都可能不同。
来自API的可能回复:
response = [
{
case: "abc",
price: "123.1234",
manager: "joe black",
duration: "3456.1231321"
},
{
case: "bbb",
price: "23.4897987",
manager: "jill smith",
duration: "78974.12156464"
}
]
or
response = [
{
case: "apl",
yield: "90.1209312093",
average: "100.123,
},
{
case: "ltl",
yield: "80666.23131313",
average: "4512.7897987,
}
]
预期结果:
response = [
{
case: "abc",
price: "123.12",
manager: "joe black",
duration: "3456.12"
},
{
case: "bbb",
price: "23.49",
manager: "jill smith",
duration: "78974.12"
}
]
or
response = [
{
case: "apl",
yield: "90.12",
average: "100.12,
},
{
case: "ltl",
yield: "80666.23",
average: "4512.79,
}
]
按照建议,您可以使用 Object.entries() 获取数组每个项目中的所有单个 key/value 对。然后您可以遍历这些并测试数字。找到的任何数字都可以四舍五入到小数点后两位,并在原始数组上更新条目。
类似于:
let response = [
{
case: "abc",
price: "123.1234",
manager: "joe black",
duration: "3456.1231321"
},
{
case: "bbb",
price: "23.4897987",
manager: "jill smith",
duration: "78974.12156464"
},
{
case: "apl",
yield: "90.1209312093",
average: "100.123"
},
{
case: "ltl",
yield: "80666.23131313",
average: "4512.7897987"
}
]
response.forEach(function(r){
let rValues = Object.entries(r);
rValues.forEach(function(e){
// e[0] is the key and e[1] is the value
let n = Number(e[1]);
if (!isNaN(n)) {
r[e[0]] = n.toFixed(2);
}
})
})
console.log(response);
您可以使用 map() 遍历数组的所有对象,并使用 Object.keys().forEach 遍历所有键。如果值字符串包含数字,则将所有值解析为浮点数。最后用 toFixed() 舍入浮点数,returns 一个舍入的浮点数转换为字符串。
let response1 = [{
case: "abc",
price: "123.1234",
manager: "joe black",
duration: "3456.1231321"
},
{
case: "bbb",
price: "23.4897987",
manager: "jill smith",
duration: "78974.12156464"
}
]
let response2 = [{
case: "apl",
yield: "90.1209312093",
average: "100.123",
},
{
case: "ltl",
yield: "80666.23131313",
average: "4512.7897987"
}
]
function round(array) {
let rounded = array;
rounded.map((el) => {
Object.keys(el).forEach(function(key) {
if (!isNaN(parseFloat(el[key]))) {
el[key] = parseFloat(el[key]);
el[key] = el[key].toFixed(2);
}
});
})
console.log(rounded);
}
round(response1);
round(response2);
我有一个来自 API 的动态响应,它每次都有不同的响应。我需要遍历一个对象数组并找到数字类型的值。然后将它们四舍五入到小数点后两位。在下面的示例中,对象键值每次都可能不同。
来自API的可能回复:
response = [
{
case: "abc",
price: "123.1234",
manager: "joe black",
duration: "3456.1231321"
},
{
case: "bbb",
price: "23.4897987",
manager: "jill smith",
duration: "78974.12156464"
}
]
or
response = [
{
case: "apl",
yield: "90.1209312093",
average: "100.123,
},
{
case: "ltl",
yield: "80666.23131313",
average: "4512.7897987,
}
]
预期结果:
response = [
{
case: "abc",
price: "123.12",
manager: "joe black",
duration: "3456.12"
},
{
case: "bbb",
price: "23.49",
manager: "jill smith",
duration: "78974.12"
}
]
or
response = [
{
case: "apl",
yield: "90.12",
average: "100.12,
},
{
case: "ltl",
yield: "80666.23",
average: "4512.79,
}
]
按照建议,您可以使用 Object.entries() 获取数组每个项目中的所有单个 key/value 对。然后您可以遍历这些并测试数字。找到的任何数字都可以四舍五入到小数点后两位,并在原始数组上更新条目。
类似于:
let response = [
{
case: "abc",
price: "123.1234",
manager: "joe black",
duration: "3456.1231321"
},
{
case: "bbb",
price: "23.4897987",
manager: "jill smith",
duration: "78974.12156464"
},
{
case: "apl",
yield: "90.1209312093",
average: "100.123"
},
{
case: "ltl",
yield: "80666.23131313",
average: "4512.7897987"
}
]
response.forEach(function(r){
let rValues = Object.entries(r);
rValues.forEach(function(e){
// e[0] is the key and e[1] is the value
let n = Number(e[1]);
if (!isNaN(n)) {
r[e[0]] = n.toFixed(2);
}
})
})
console.log(response);
您可以使用 map() 遍历数组的所有对象,并使用 Object.keys().forEach 遍历所有键。如果值字符串包含数字,则将所有值解析为浮点数。最后用 toFixed() 舍入浮点数,returns 一个舍入的浮点数转换为字符串。
let response1 = [{
case: "abc",
price: "123.1234",
manager: "joe black",
duration: "3456.1231321"
},
{
case: "bbb",
price: "23.4897987",
manager: "jill smith",
duration: "78974.12156464"
}
]
let response2 = [{
case: "apl",
yield: "90.1209312093",
average: "100.123",
},
{
case: "ltl",
yield: "80666.23131313",
average: "4512.7897987"
}
]
function round(array) {
let rounded = array;
rounded.map((el) => {
Object.keys(el).forEach(function(key) {
if (!isNaN(parseFloat(el[key]))) {
el[key] = parseFloat(el[key]);
el[key] = el[key].toFixed(2);
}
});
})
console.log(rounded);
}
round(response1);
round(response2);