【问题标题】:how to replace comma with dot如何用点替换逗号
【发布时间】:2016-09-05 07:25:23
【问题描述】:

我的 json 中有逗号值,我希望当我得到这些值时,我将它们放在点中,所以基本上我想将我的逗号值转换为点值..我的 json 看起来和它总是固定的,我会得到val003 处的逗号值。 我知道像 var new = new.replace(/,/g, '.') 这样的事情。但是我如何在此处指定我的 val003 进行转换。提前谢谢你

我的html

<!doctype html>

<html>

<head>

    <meta charset="UTF-8">
    <meta content="utf-8" http-equiv="encoding">
      <div id="below">
        <div id="chart"></div>
    </div>
    <script>
        var jsonURL = 'avb.json';
        var myData = [];
        var fliterdata = [];
        var tempdata = [];
        var selectop = "";
        var selectDate = false;
        var chartType = chartType || 'bar';

        function filterJSON(json, key, value) {
            var result = [];
            for (var foo in json) {
                var extractstr = json[foo][key] ;
                extractstr=String(extractstr);

                if (extractstr.slice(3)== value) {

                    result.push(json[foo]);

               }
            }
            return result;
        }
        function selectValue(d) {    
            switch (selectop) { //d object select particular value for Y axis
                case "01":
                    return d.val001;
                    break;
                case "02":
                    return d.val002;
                    break;
                case "03":
                    return d.val003;
                    break;
                case "04":
                    return d.val004;
                    break;
               default:
                    //console.log("default");
                    return d.val001;
            }


        }

      var line = d3.svg.line()
                .x(function(d) {
                    return xScale(d.date);
                })
                .y(function(d) {
                    return yScale(selectValue(d));
                })
                .interpolate("monotone")
                .tension(0.9);




            yScale.domain([0, d3.max(tempData, function(d) {
                return +selectValue(d);
            })]);

            var svg = d3.select('#chart').append('svg')
                    .attr("width", width + margin.left + margin.right)
                    .attr("height", height + margin.top + margin.bottom)
                    .append("g")
                    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


            if (chartType == 'bar') {
                svg
                        .selectAll(".bar") //makes bar
                        .data(tempData)
                        .enter().append("rect")
                        .attr("class", "bar")
                        .style("fill", "teal")
                        .attr("x", function(d) {
                            return xScale(d.date);
                        }).attr("width", xScale.rangeBand())
                        .attr("y", function(d) {


                            return yScale(selectValue(d));
                        }).attr("height", function(d) {

                    console.log("as", d.value);
                    return height - yScale(selectValue(d));
                })
            }

            if (chartType == 'line') {
                svg.append("path") // Add the line path.
                        .data(tempData)
                        .attr("class", "line")
                        .attr("d", line(tempData));
            }

        }
        d3.json(jsonURL, function(data) {

            myData = data; //data from json in mydata
           d.val003.replace(",",".")
            myData.forEach(function(d) {

                d.date = new Date(d.date);
                d.date = new Date(d.date + " UTC");

            });

            $("#listbox").on("click", function() {

                var key = $(this).val();
                console.log("key:", key);
                var value = $('#listbox option:selected').text();
                console.log("vaue:", value);

                selectop = String(key);

                selectop = selectop.slice(-2);
                console.log("mydata: ", myData);
                console.log("selectops:", selectop);

                fliterdata = filterJSON(myData, key, value); //selected value from user and picks the whole element that contains that attribute

                console.log("fliterdata: ", fliterdata);
                tempData = fliterdata; //graph made by temp data
                if (selectDate)
                    render(true);
            });
        });

        function selectChartType(type) {
            chartType = type;
            render(true);
        }
    </script>
    </body>
</div>
</body>
</html>

【问题讨论】:

  • .replace(",",".") 您可以与 json 数据 d.val003.replace(",",".") 一起使用
  • 你的意思是我应该在我的“switch case staments”中使用?
  • 你可以在那里使用,如果每个 d.val003 都从那里返回,并且它似乎正在这样工作,试一试
  • 它说未解析的变量 val003..见我上面指定的
  • 你可以为你的代码做一个小提琴然后我可以试一试

标签: html arrays json


【解决方案1】:

试试这个,

return d.val003.toString().replace(",",".");

【讨论】:

  • 我应该在哪里写这个
  • 在你的 switch return 或者你可以放在任何地方当你返回这个值。
【解决方案2】:

你可以简单地请求一个 JSon 对象中的值——它几乎可以作为 JavaScript 中的一个对象。

所以如果你有你的 JSon 对象,让我们称之为 json 你可以简单地做:

var url = *your url*, json;

// retrieve the json object from a URL
$.getJSON(url, function (response) {$
    json = response;
});

// reassing val003 with the corrected string
json.val003 = json.val003.replace(",", ".")

我相信这应该可行。

【讨论】:

  • 如果你有 json 对象,只有最后一行很重要。
【解决方案3】:

如果要替换的始终是十进制数字中的逗号,则可以在整个 json 字符串中搜索替换序列“number”“comma”“number”,例如:

([0-9]),([0-9])

并将其替换为:

$1.$2

$1 和 $2 是逗号前后找到的数字的占位符。 您可以使用此站点进行在线测试: http://www.regexe.com/

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    相关资源
    最近更新 更多