score:5
Accepted answer
fetch returns a response
to get to the json, you need to await response.json()
like so
async mounted() {
this.loaded = false
try {
const response = await fetch("http://localhost:8000/api/report/" + this.$route.params.id)
const {report: {usage: {license}}} = await response.json();
this.chartData = license
this.loaded = true
} catch (e) {
console.error(e)
}
}
Here's my last answer combined with this answer in a working snippet
class Player {
constructor(player_id, score) {
this.player_id = player_id;
this.scores = [score];
this.total = score;
}
addScore(score) {
this.total += score;
this.scores.push(score);
return score;
}
get average() {
return this.scores.length ? this.total / this.scores.length : 0;
}
resetScore() {
this.scores = [];
this.score = 0;
}
};
class LeaderBoard {
constructor() {
this.players = {};
}
addScore(player_id, score) {
if (!this.players[player_id]) {
this.players[player_id] = new Player(player_id, score);
} else {
this.players[player_id].addScore(score);
}
return this.players[player_id].average.toFixed(1);
}
top = (num_players) => {
return Object.values(this.players).sort((a, b) => (a.average - b.average)).slice(0, num_players);
}
};
let x = new LeaderBoard();
x.addScore(1, 4);
x.addScore(2, 3);
x.addScore(3, 2);
x.addScore(4, 1);
console.log(x.top(3));
Source: stackoverflow.com
Related Query
- what is the correct way to destructure a nested json object with fetch?
- What is the correct way to send 2 parameters in socket.io using lambdas? (I get undefined when printing in client and server)
- What's the most effective way to implement a radar plot with 50 points at arbitrary locations using chart.js
- How to rename properties in objects that are in an array. What is the right way to solve the task?
- Populating Chart.Js line graph with multiple lines using data from a JSON Object
- Charts in Angular js with JSON Object
- How to write better code in es6 for formatting an object with array values
- Problem with script src for Chart.js. The CDN for Chart.js funtions fine, so my code is ok. Somehow I'm not linking the file correctly
- What is the correct format for time data in chartjs?
- what is wrong with my code ? java script chart position
- Chart.js - Multiple JSON data object array [{x: Date, y: count}....] representing each dataset of the chart
- ChartJs not working with this JSON Object
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
- How to make dynamic chart with the next view? What libraries allows to customise exactly this like in my design?
- Can I interpret Django object values as JSON data within the JS script in my HTML?
- What is the order in which the hooks (plugins) of Chart.js are executed?
- Testing Chart.js with Jest/Enzyme - Failed to create chart: can't acquire context from the given item
- Chartjs 2.7.3: Set Y data at the correct X position axis
- Live Update Callback -> afterTitle with Array via JSON file
- chart.js v2: Align time scale labels with the center of the bars
- Drawing line chart in chart.js with json response
- Grouping the object by key for Chartjs bar chart
- What is the syntax for require.config for chartjs-plugin-streaming and dependencies using RequireJS?
- Use JSON file with chart.js
- Maintain Y axis scale in graph with scroll in the X axis when the dataset grows bigger. Aspect-ratio problem
- How can I trigger the hover mode from outside the chart with charts.js 2?
- Multiple lines / data series from JSON file with Chart.js
- Chartjs Nested Doughnut Layers With Different Thickness
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- Chart.js load new data from saved JSON object
More Query from same tag
- Why is my data on chartjs not starting from the data that it should
- Creating categories with different widths in chart.js
- Chart.js using the value of certain data in external json file
- Can we draw a Line Chart with both solid and dotted line in it?
- Chart.js chart doesn't render when using Angular 2
- How to get the length (height) of the vertical bar in Chart.js
- Is possible convert an associative array in indexed array in php?
- Click on interactive chart.js bar chart and get value for labels and groups in JS
- chart is not getting updated from the values it received from Jquery
- ChartJS Bar Graph Gap
- Tilting the labels of the x axis to some degrees in chart.js
- ChartJS - Highlight grouped bars
- ChartJS: Full Date for xAxis Labels?
- How can I make bar charts symmetrical in chart.js?
- Chart js show levels on top. (Bar chart with Stacked group)
- How to increase the spacing between labels and the chart
- Is there a way to apply css on chart.js V3.7.0 tooltip without it being custom or external?
- Stacked areachart with percents values on the y axis using Charts.js and Angular 8
- Hide Legend in Chart.js V3.7.1
- ChartJS doughnut colors not showing from a Flask app. All gray
- How do I use ChartJS with a background color in the space between two line charts?
- how get data from given string in javascript?
- how to reduce list chart to one and use select dropdown to show selection without refresh page?
- ChartJS Horizontal Bar is displaying backwards
- angular-chartjs line chart TypeError: t.merge is not a function
- Charts.js Y-Axis whole Numbers
- Add \n or line break on chart.js (pie chart) labels
- Custom tooltip callback on one dataset (chartjs v 2.5)
- Map through datasets in chartjs
- ChartJS how to sync animations when page loads