简体   繁体   中英

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

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:

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:

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.

No destructuring required (it doesn't work that way anyway)

let arr = obj.map(e => [new Date(e.dateTime), +e[valToChart]]);

Should do what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM