1

I have a problem. There is a specific page, whick has a <script>, which I need to tweak some numbers in.

var series = [{"color": "#666666", "data": [[25.25, 0.0]], "label": "\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435"}, {"color": "#b72121", "data": [[1, 0.0], [2, 0.0], [3, 0.0], [4, 0.0], [5, 0.0], [6, 0.0], [7, 0.0], [8, 0.0], [9, 0.0], [10, 0.88], [11, 0.0], [12, 0.0], [13, 0.0], [14, 0.0], [15, 0.95], [16, 0.0], [17, 0.0], [18, 1.0], [19, 0.0], [20.25, 0.14894736842105263]], "label": "\u0418\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0435"}, {"color": "#600101", "data": [[21.5, 0.0], [22.5, 0.0], [23.75, 0.0]], "label": "\u041f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0435 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435"}, {"color": "#b72121", "data": [[26.75, 0.056600000000000004]], "label": "\u0418\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0435-grade_breakdown"}];

the data

( "data": [[1, 0.0], [2, 0.0], [3, 0.0], [4, 0.0], [5, 0.0], [6, 0.0], [7, 0.0], [8, 0.0], [9, 0.0], [10, 0.88], [11, 0.0], [12, 0.0], [13, 0.0], [14, 0.0], [15, 0.95], [16, 0.0], [17, 0.0], [18, 1.0], [19, 0.0], [20.25, 0.14894736842105263]] )

needs to be changed to 1.0 everywhere. And I have no idea how to do this as I'm not an expert in JS nor Greasemonkey.

1

2 Answers 2

2

Lets first take a look at the structure of your variable:

  var series = [
        {
              "color": "#666666",
              "data": [[25.25, 0.0]],
              "label": "\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435"
        },
        {
              "color": "#b72121",
              "data": [[1, 0.0], [2, 0.0], [3, 0.0], [4, 0.0], [5, 0.0], [6, 0.0], [7, 0.0], [8, 0.0], [9, 0.0], [10, 0.88], [11, 0.0], [12, 0.0], [13, 0.0], [14, 0.0], [15, 0.95], [16, 0.0], [17, 0.0], [18, 1.0], [19, 0.0], [20.25, 0.14894736842105263]],
              "label": "\u0418\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0435"
        },
        {
              "color": "#600101",
              "data": [[21.5, 0.0], [22.5, 0.0], [23.75, 0.0]],
              "label": "\u041f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0435 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435"
        },
        {
              "color": "#b72121",
              "data": [[26.75, 0.056600000000000004]],
              "label": "\u0418\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0435-grade_breakdown"
        }
  ];

Okay, what do we have? series is an array containing multiple tuples, you can access them by series[0], series[1] and so on. A tuple is an object containing three key-value pairs. For example you can access color of the first tuple by series[0].color. The values of color and label are Strings. The value of data are again arrays. For the second tuple you can access the [2, 0.0] array by using series[1].data[1]. Those values are again arrays. If you want to access the value 2 of the previous element it is series[1].data[1][0].

Okay, for changing all data values you just need to iterate:

  for (var i = 0; i < series.length; i++) {
        var tuple = series[i];
        var data = tuple.data;
        for (var j = 0; j < data.length; j++) {
              var dataContainer = data[j];
              for (var k = 0; k < dataContainer.length; k++) {
                    dataContainer[k] = 1.0;
              }
        }
  }

Of course there are some methods that do that job for you but in my opinion it is important that you understand how it works in principle.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you want to replace all the 0.0 by 1.0, apply that after your declaration:

series.forEach(function(element){
  element.data.forEach(function(data){
     data[1]= 1;
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.