JS:在一个深度嵌套的对象中,对所有数字数据进行四舍五入的最佳方法是什么


let data = {
"enable": false,
"show": false,
"showCount": false,
"work1": {
"active": true,
"components": [
{
"ID": "xxxxxxxxxxxxxx",
"filled": true,
"size": {
"width": 50,
"height": 50
},
"frame": {
"x": 333.01068097610835,
"y": 223.99888508072843,
"width": 50,
"height": 50
}
},
{
"ID": "yyyyyyyyyyyyyy",
"filled": true,
"size": {
"width": 50,
"height": 50
},
"frame": {
"x": 648.0187300370513,
"y": 270.99747284965105,
"width": 50,
"height": 50
}
}
],
"annotations": [
{
"ID": "xxxxxxxxxxxxx",
"points": [
{
"x": 442.0133605468252,
"y": 522.0119330635505
},
{
"x": 443.01438609939953,
"y": 521.0129082875666
},
{
"x": 443.01438609939953,
"y": 518.0128339596151
},
{
"x": 442.0143605468252,
"y": 517.0128091836314
},
{
"x": 442.0143605468252,
"y": 515.0127596316637
},
],
"tolerance": 1
}
],
"shapes": []
}
}

我想将Math.round((应用于上面对象中的所有数字数据。有没有任何库可以帮助我运行循环并对所有数字数据应用四舍五入。现在,我正在运行一个循环,并将Math.round应用于特定键中的数字数据。

例如

"frame": {
"x": 333.01068097610835,
"y": 223.99888508072843,
"width": 50,
"height": 50
}

应转换为

"frame": {
"x": 333,
"y": 224,
"width": 50,
"height": 50
}

我试过这个:

得到";错误:未捕获类型错误:无法分配给只读属性">


function reduceSize() {
for (let key in data) {
if (typeof (data[key]) === "object") {
if (data[key]["annotations"] && data[key]["annotations"] instanceof Array) {
roundOffAnnotations(data[key]["annotations"]);
}
}
}
console.log(data);
}
function roundOffAnnotations(annotations) {
if (annotations && annotations instanceof Array) {
for (let index = 0; index < annotations.length; index++) {
let annotationAtIndex = annotations[index];
let roundOffPoints = [];
if (annotationAtIndex && annotationAtIndex["points"] instanceof Array) {
for (let point of annotationAtIndex["points"]) {
roundOffPoints.push({
x: Math.round(point.x),
y: Math.round(point.y)
});
}
}
annotations[index]["points"] = roundOffPoints; // getting error in this line 
// Error: Uncaught TypeError: Cannot assign to read only property
}
}
}

如果您的数据是JSON安全的(即没有函数或特殊对象(,您可以简单地将其重新编码到JSON:

newData = JSON.parse(JSON.stringify(data, function (key, val) {
if (typeof val === 'number')
return Math.round(val);
return val;
}))

您可以执行一个简单的递归:

  1. 对任何数值进行舍入
  2. 映射任何阵列
  3. 转换任何对象的值
  4. 不动任何其他东西

无突变:

const round = value => {
if (typeof value === "number")
return Math.round(value);

if (Array.isArray(value))
return value.map(round);

if (typeof value === "object" && value !== null)
return Object.fromEntries(
Object.entries(value)
.map(([k, v]) => [k, round(v)])
);

return value
}
let data = { "enable": false, "show": false, "showCount": false, "work1": { "active": true, "components": [ { "ID": "xxxxxxxxxxxxxx", "filled": true, "size": { "width": 50, "height": 50 }, "frame": { "x": 333.01068097610835, "y": 223.99888508072843, "width": 50, "height": 50 } }, { "ID": "yyyyyyyyyyyyyy", "filled": true, "size": { "width": 50, "height": 50 }, "frame": { "x": 648.0187300370513, "y": 270.99747284965105, "width": 50, "height": 50 } } ], "annotations": [ { "ID": "xxxxxxxxxxxxx", "points": [ { "x": 442.0133605468252, "y": 522.0119330635505 }, { "x": 443.01438609939953, "y": 521.0129082875666 }, { "x": 443.01438609939953, "y": 518.0128339596151 }, { "x": 442.0143605468252, "y": 517.0128091836314 }, { "x": 442.0143605468252, "y": 515.0127596316637 }, ], "tolerance": 1 } ], "shapes": [] } };
console.log(round(data))

在适当位置修改对象:

const round = value => {
if (typeof value === "number")
return Math.round(value);

if (Array.isArray(value))
value.forEach((el, index) => value[index]= round(el));

if (typeof value === "object" && value !== null)
Object.entries(value)
.forEach(([k, v]) => value[k] = round(v))
return value
}
let data = { "enable": false, "show": false, "showCount": false, "work1": { "active": true, "components": [ { "ID": "xxxxxxxxxxxxxx", "filled": true, "size": { "width": 50, "height": 50 }, "frame": { "x": 333.01068097610835, "y": 223.99888508072843, "width": 50, "height": 50 } }, { "ID": "yyyyyyyyyyyyyy", "filled": true, "size": { "width": 50, "height": 50 }, "frame": { "x": 648.0187300370513, "y": 270.99747284965105, "width": 50, "height": 50 } } ], "annotations": [ { "ID": "xxxxxxxxxxxxx", "points": [ { "x": 442.0133605468252, "y": 522.0119330635505 }, { "x": 443.01438609939953, "y": 521.0129082875666 }, { "x": 443.01438609939953, "y": 518.0128339596151 }, { "x": 442.0143605468252, "y": 517.0128091836314 }, { "x": 442.0143605468252, "y": 515.0127596316637 }, ], "tolerance": 1 } ], "shapes": [] } };
round(data);
console.log(data);

最新更新