Reputation: 649
I did a code to return a json, example bellow:
[
{
"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"
}
]
}
]
These json what I have as result. I would like to change the decimal numbers in Idea value to percentage:
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%
So the result become:
{
"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%"
}
]
}
My code is the following:
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%"
}
Example final_Matrix column dataset:
53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245
Someone can help me please to change my code to convert the decimal number to percentage ? Thank you
Upvotes: 0
Views: 1967
Reputation: 241
1) loop to your raw data
2) on the key 'label', search the array with 'filter'==='Idea'
3) perform split and join to convert
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);
Upvotes: 0
Reputation: 11001
Here is the object conversion using string split
, join
methods.
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));
Upvotes: 0
Reputation: 33726
You can loop, split according to the strings, reducing the array of tokens (those separated by comma ,
) and finally rebuild the tokens with the transformed percentage values.
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; }
Upvotes: 0
Reputation: 15247
You can first split the string containing your datas (queryResult.final_Matrix[index]
) to get each parts separatly, then, for each parts, split again using ":"
and apply some math (multiply by 100) to get the percentage :
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);
Upvotes: 2