将小数转换为 javascript 中的百分比
Convert a decimal number to percentage in javascript
我编写了一个代码 return a 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"
}
]
}
]
这些 json 我得到的结果。
我想将创意值中的小数改为百分比:
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
有人可以帮助我更改我的代码以将小数转换为百分比吗?
谢谢
您可以先拆分包含数据的字符串 (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);
你可以循环,根据字符串拆分,减少令牌数组(用逗号分隔的令牌 ,
),最后用转换后的百分比值重建令牌。
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; }
这里是使用字符串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));
1) 循环到您的原始数据
2) 在关键字 'label' 上,用 'filter'==='Idea'
搜索数组
3) 执行split和join转换
data=[{...'label':{'filter':'Idea'...}...},{}];
var labels=[],valueInd=[],value=[],div2=[];
data.map((element)=>{
labels = element.labels;
valueInd=labels.findIndex(elem=>elem.filter==='Idea');
value=labels[valueInd].value;
value=value.split(' . ')[0]+ ' . '+value.split(' . ')[1].split(',').map((elem)=>{
var div2=elem.split(':');
return div2.length>1?div2[0]+':'+(Number(div2[1])*100).toFixed(2)+'%':div2[0];
}).join(',');
labels[valueInd].value=value;
});
console.log(data);
我编写了一个代码 return a 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"
}
]
}
]
这些 json 我得到的结果。 我想将创意值中的小数改为百分比:
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
有人可以帮助我更改我的代码以将小数转换为百分比吗? 谢谢
您可以先拆分包含数据的字符串 (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);
你可以循环,根据字符串拆分,减少令牌数组(用逗号分隔的令牌 ,
),最后用转换后的百分比值重建令牌。
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; }
这里是使用字符串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));
1) 循环到您的原始数据
2) 在关键字 'label' 上,用 'filter'==='Idea'
搜索数组
3) 执行split和join转换
data=[{...'label':{'filter':'Idea'...}...},{}];
var labels=[],valueInd=[],value=[],div2=[];
data.map((element)=>{
labels = element.labels;
valueInd=labels.findIndex(elem=>elem.filter==='Idea');
value=labels[valueInd].value;
value=value.split(' . ')[0]+ ' . '+value.split(' . ')[1].split(',').map((elem)=>{
var div2=elem.split(':');
return div2.length>1?div2[0]+':'+(Number(div2[1])*100).toFixed(2)+'%':div2[0];
}).join(',');
labels[valueInd].value=value;
});
console.log(data);