我做了一个代码来返回一个json,例如:
[
{
"id": "12345",
"header": "<a class=\"card-link\" href=\"http://www.google.com\" target=\"_blank\"> 12345</a>- solved-1",
"title": "Training Summary Report",
"description": "",
"link": "",
"labels": [
{
"filter": "type",
"value": "course 1"
},
{
"filter": "Subject",
"value": "Sub. 1239"
},
{
"filter": "Idea",
"value": "Idea . 53"
}
]
}
{
"id": "12345",
"header": "<a class=\"card-link\" href=\"http://www.google.com\" target=\"_blank\"> 12345</a>- solved-1",
"title": "Training Summary Report",
"description": "",
"link": "",
"labels": [
{
"filter": "type",
"value": "course 1"
},
{
"filter": "Subject",
"value": "Sub. 1239"
},
{
"filter": "Idea",
"value": "Idea . 53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245"
}
]
}
]这些都是我的结果。我想将概念值中的十进制数字更改为百分比:
53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245
Expect result:
53-34-98:0,45%, 98-11-00:9.8%, 44-22-88:9.875%, 22-98-90:32%结果是:
{
"id": "12345",
"header": "<a class=\"card-link\" href=\"http://www.google.com\" target=\"_blank\"> 12345</a>- solved-1",
"title": "Training Summary Report",
"description": "",
"link": "",
"labels": [
{
"filter": "type",
"value": "course 1"
},
{
"filter": "Subject",
"value": "Sub. 1239"
},
{
"filter": "Idea",
"value": "Idea . 53-34-98:0,45%, 98-11-00:9.8%, 44-22-88:9.875%, 22-98-90:32%"
}
]
}我的代码如下:
result = ""
if(queryResult.final_Matrix[index] == null){
if(queryResult.idea[index] != null){
result = "Idea. " + queryResult.idea[index]
}
}
else{
result = "Idea. " + queryResult.final_Matrix[index] // the result of this line is: "value": "Idea . 53-34-98:0,45%, 98-11-00:9.8%, 44-22-88:9.875%, 22-98-90:32%"
}示例final_Matrix列数据集:
53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245有人可以帮我把我的代码转换成十进制的百分比吗?谢谢
发布于 2020-01-22 10:02:49
您可以首先拆分包含数据的字符串(queryResult.final_Matrix[index]),以分离每个部分,然后,对于每个部分,使用":"再次拆分,并应用一些数学(乘以100)得到百分比:
let input = "53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245";
let times = input.split(", ");
const result = [];
times.forEach((elem) =>
{
const tempArr = elem.split(":");
result.push(tempArr[0] + ":" + (Math.round((tempArr[1] * 100) * 100) / 100) + "%");
});
let finalResult = result.join(", ");
console.log(finalResult);
发布于 2020-01-22 10:07:58
您可以循环,根据字符串进行拆分,减少标记数组(用逗号,分隔的标记),最后用转换后的百分比值重新构建令牌。
let arr = [ { "id": "12345", "header": "<a class=\"card-link\" href=\"http://www.google.com\" target=\"_blank\"> 12345</a>- solved-1", "title": "Training Summary Report", "description": "", "link": "", "labels": [ { "filter": "type", "value": "course 1" }, { "filter": "Subject", "value": "Sub. 1239" }, { "filter": "Idea", "value": "Idea . 53" } ] }, { "id": "12345", "header": "<a class=\"card-link\" href=\"http://www.google.com\" target=\"_blank\"> 12345</a>- solved-1", "title": "Training Summary Report", "description": "", "link": "", "labels": [ { "filter": "type", "value": "course 1" }, { "filter": "Subject", "value": "Sub. 1239" }, { "filter": "Idea", "value": "Idea . 53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245" } ] }];
arr.forEach(({labels}) => {
labels.forEach(label => {
let [_, value] = label.value.split("Idea . ");
if (value) {
label.value = "Idea . " + value.split(",").reduce((a, t) => {
let [str, perc] = t.split(":");
if (perc) str += ":" + (Number(perc.trim()) * 100).toFixed(2) + "%"
return a.concat(str);
}, []).join();
}
});
});
console.log(arr);.as-console-wrapper { max-height: 100% !important; top: 0; }
发布于 2020-01-22 10:26:21
下面是使用string split、join方法进行对象转换的方法。
const convert = item => {
const labels = item.labels.map(label => {
if (label.value.includes("Idea")) {
return {
...label,
value: label.value
.split(",")
.map(val => {
const strs = val.split(":");
const last = strs.pop();
strs.push(`${Math.round(Number(last) * 100 * 100) / 100}%`);
return strs.join(":");
})
.join(",")
};
} else {
return { ...label };
}
});
return {
...item,
labels
};
};
const arr = [
{
id: "12345",
header:
'<a class="card-link" href="http://www.google.com" target="_blank"> 12345</a>- solved-1',
title: "Training Summary Report",
description: "",
link: "",
labels: [
{
filter: "type",
value: "course 1"
},
{
filter: "Subject",
value: "Sub. 1239"
},
{
filter: "Idea",
value: "Idea . 53"
}
]
},
{
id: "12345",
header:
'<a class="card-link" href="http://www.google.com" target="_blank"> 12345</a>- solved-1',
title: "Training Summary Report",
description: "",
link: "",
labels: [
{
filter: "type",
value: "course 1"
},
{
filter: "Subject",
value: "Sub. 1239"
},
{
filter: "Idea",
value:
"Idea . 53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245"
}
]
}
];
console.log(arr.map(convert));
https://stackoverflow.com/questions/59856851
复制相似问题