首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在javascript中将十进制数转换为百分比

在javascript中将十进制数转换为百分比
EN

Stack Overflow用户
提问于 2020-01-22 09:52:37
回答 4查看 1.4K关注 0票数 0

我做了一个代码来返回一个json,例如:

代码语言:javascript
复制
[
  {
    "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"
      }
    ]
  }
]

这些都是我的结果。我想将概念值中的十进制数字更改为百分比:

代码语言:javascript
复制
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%

结果是:

代码语言:javascript
复制
{
    "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%"
      }
    ]
  }

我的代码如下:

代码语言:javascript
复制
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列数据集:

代码语言:javascript
复制
53-34-98:0.0045, 98-11-00:0.09856, 44-22-88:0.09875, 22-98-90:0.3245

有人可以帮我把我的代码转换成十进制的百分比吗?谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-01-22 10:02:49

您可以首先拆分包含数据的字符串(queryResult.final_Matrix[index]),以分离每个部分,然后,对于每个部分,使用":"再次拆分,并应用一些数学(乘以100)得到百分比:

代码语言:javascript
复制
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);

票数 2
EN

Stack Overflow用户

发布于 2020-01-22 10:07:58

您可以循环,根据字符串进行拆分,减少标记数组(用逗号,分隔的标记),最后用转换后的百分比值重新构建令牌。

代码语言:javascript
复制
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);
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 0
EN

Stack Overflow用户

发布于 2020-01-22 10:26:21

下面是使用string splitjoin方法进行对象转换的方法。

代码语言:javascript
复制
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));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59856851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档