I am trying to pull out the license array of numbers for a chart.js
The shape of the API report data is:
{
"report": {
"usage": {
"chartLabels": [
"'1-Mar', '2-Mar', '3-Mar', '4-Mar', '5-Mar', '6-Mar', '7-Mar', '8-Mar', '9-Mar', '10-Mar', '11-Mar', '12-Mar', '13-Mar', '14-Mar', '15-Mar', '16-Mar', '17-Mar', '18-Mar', '19-Mar', '20-Mar', '21-Mar', '22-Mar', '23-Mar', '24-Mar', '25-Mar', '26-Mar', '27-Mar', '28-Mar', '29-Mar', '30-Mar', '31-Mar'"
],
"license": [
"'3', '50', '56', '53', '60', '56', '47', '3', '39', '67', '60', '57', '61', '61', '8', '47', '49', '51', '49', '45', '42', '3', '3', '4', '4', '3', '3', '3', '3', '3', '4'"
],
}
}
}
Is it possible to destructure like so with fetch? I am not getting anything back with a console.log(license)
async mounted () {
this.loaded = false
try {
const { report: {usage: { license } } } = await fetch("http://localhost:8000/api/report/" + this.$route.params.id)
this.chartData = license
this.loaded = true
} catch (e) {
console.error(e)
}
}
Thanks for any help!
Nested Object and Array Destructuring Here's another example with an array of objects: You can destructure as deeply as you like: As you can see, keys a , b , and c are not implicitly defined, even though we pulled out nested values, firstElemOfC and remainingElementsOfC , from the array at c .
Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.
Nested JSON is simply a JSON file with a fairly big portion of its values being other JSON objects. Compared with Simple JSON, Nested JSON provides higher clarity in that it decouples objects into different layers, making it easier to maintain.
To destroy the structure of something. verb. 2. To dismantle.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With