Passing string to function as an object's property in “let” gives “Unexpected token }”

Shave

New Member
#1
I'm trying to make a 'generic' charting function which takes a series of parameters for charting different data values with different labels and baseline values. I've created the following function to handle this:
Mã:
function ProcessChart(obj, valToChart, desc, element, baselineVal) {
    let arr = obj.map(e => {
        let { dateTime, [valToChart] } = e; // ERROR: Unexpected token }
        return [new Date(dateTime), +[valToChart]];
    });

    arr.unshift(["DateTime", desc]);
    var data = google.visualization.arrayToDataTable(arr)

    var options = {
        title: desc + " for " + obj[0].computerName,
        curveType: 'function',
        legend: { position: 'bottom' },
        animation: {
            startup: false,
            duration: 500
        },
        vAxis: {
            baseline: baselineVal,
            baselineColor: 'red'
        }
    };

    var chart = new
    google.visualization.LineChart(document.getElementById(element));
    chart.draw(data, options);
}
Caller:
Mã:
google.charts.setOnLoadCallback(ProcessChart(obj, "averageJitterInMs", "Average Jitter (ms)", "line-jitter-8hr", 10))
google.charts.setOnLoadCallback(ProcessChart(obj, "roundTripLatencyInMs", 'Round-Trip Latency (ms)', 'line-delay-8-hr', 8))
The problem is my valToChart being passed gives an error:
Unexpected token }
Is there another way I should be expressing this as a dynamic property on my obj object? I've tested this code with 'hard-coded' values in each place and it works fine.
 

Admin

Administrator
Thành viên BQT
#2
No destructuring required (it doesn't work that way anyway)
Mã:
let arr = obj.map(e => [new Date(e.dateTime), +e[valToChart]]);
Should do what you want
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top