How to grab variables from a data log - javascript
I'll preface this question by stating that I am very new to Javascript and programming as a whole as I'm a current first year student. I am attempting to create an app which makes use of data given to me by an API. However, this API only returns the data as one whole String with different subsections.
This is how the return looks when logged:
User {
id: 'redacted',
username: 'Despacito II',
platform: 'PC',
url: 'https://fortnitetracker.com/profile/pc/Despacito II',
stats:
{ solo:
Mode {
score: 101032,
kd: 0.35,
matches: 916,
kills: 324,
kills_per_match: 0.35,
score_per_match: 110.3,
wins: 1,
top_3: 1,
top_5: 2,
top_6: 4,
top_12: 8,
top_25: 231 },
duo:
Mode {
score: 29650,
kd: 0.47,
matches: 198,
kills: 92,
kills_per_match: 0.46,
score_per_match: 149.75,
wins: 2,
top_3: 2,
top_5: 23,
top_6: 27,
top_12: 104,
top_25: 158 },
squad:
Mode {
score: 166404,
kd: 0.42,
matches: 795,
kills: 323,
kills_per_match: 0.41,
score_per_match: 209.31,
wins: 31,
top_3: 120,
top_5: 151,
top_6: 488,
top_12: 790,
top_25: 1580 },
current_solo:
Mode {
score: 16660,
kd: 0.62,
matches: 128,
kills: 80,
kills_per_match: 0.62,
score_per_match: 130.16,
wins: 0,
top_3: 0,
top_5: 0,
top_6: 0,
top_12: 0,
top_25: 34 },
current_duo:
Mode {
score: 11907,
kd: 0.78,
matches: 60,
kills: 45,
kills_per_match: 0.75,
score_per_match: 198.45,
wins: 2,
top_3: 2,
top_5: 12,
top_6: 16,
top_12: 55,
top_25: 87 },
current_squad:
Mode {
score: 24822,
kd: 0.56,
matches: 130,
kills: 70,
kills_per_match: 0.54,
score_per_match: 190.94,
wins: 4,
top_3: 19,
top_5: 23,
top_6: 69,
top_12: 115,
top_25: 230 },
lifetime:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ] } }
I was wondering how I could take this return and retrieve variables from it. For example, have a new variable called 'kdSolo' and have it equal 0.35 as per the APIs return?
I'd also love to know if there is a specific term for this and what it is for future reference and research. Sorry again for the noobie question.
if you want to walk the current object tree then you could simply do:
var kdSolo = User.stats.solo.Mode.kd
// kdSolo will now be 0.35
The reason I asked about the JSON is because what you have above is not a valid JSON object.
Related
How to update an object property with MongoDB according to others properties?
I try to update a sport tournament pool table in a MongoDB collection. Pool tables data look that way : { id: 1, pool: 'A', teams: [ {name:'New-Zealand', wins: 4, draws: 0, losses: 0, PointsFor: 143, PointsAgainst: 35, DIFF: 0}, {name:'France', wins: 3, draws: 0, losses: 1, PointsFor: 129, PointsAgainst: 41, DIFF: 0}, {name:'Italy', wins: 2, draws: 0, losses: 2, PointsFor: 88, PointsAgainst: 75, DIFF: 0}, {name:'Uruguay', wins: 0, draws: 1, losses: 3, PointsFor: 50, PointsAgainst: 102, DIFF: 0}, {name:'Namibia', wins: 0, draws: 1, losses: 3, PointsFor: 53, PointsAgainst: 113, DIFF: 0} ] }, { id: 2, pool: 'B', teams: [ {name:'South-Africa', wins: 3, draws: 1, losses: 0, PointsFor: 132, PointsAgainst: 32, DIFF: 0}, {name:'Ireland', wins: 3, draws: 1, losses: 0, PointsFor: 126, PointsAgainst: 35, DIFF: 0}, {name:'Scotland', wins: 2, draws: 0, losses: 2, PointsFor: 95, PointsAgainst: 69, DIFF: 0}, {name:'Tonga', wins: 1, draws: 0, losses: 3, PointsFor: 66, PointsAgainst: 91, DIFF: 0}, {name:'Romania', wins: 0, draws: 0, losses: 4, PointsFor: 30, PointsAgainst: 110, DIFF: 0} ] } ]; I have written the functions that increment wins, draws, losses, PointsFor and PointsAgainst according to some forecasts stored in an other collection. Now, I'm trying to change DIFF properties so that for each object in teams array, DIFF = PointsFor - PointsAgainst. I'm new to MongoDB and I thought about using the '$subtract' method but I don't understand how aggregation operations work. Thanks in advance for your assistance.
You can do it via below update/aggregation operation (4.2+): db.collection.update({}, [ { "$addFields": { "teams": { "$map": { "input": "$teams", "as": "t", "in": { "$mergeObjects": [ "$$t", { DIFF: { "$subtract": [ "$$t.PointsFor", "$$t.PointsAgainst" ] } } ] } } } } } ], { multi: true }) Explained: Use $addFields/$map/$mergeObjects/$subtract to walk over all teams array elements and update the DIFF field. Use {multi:true} update option to update all documents in your collection Playground
Having trouble accessing the nested values of my object. JavaScript
So I am trying to create a function that will list all the stats of a specific player in below object. I am simply trying to return the nested object associated with the playerName itself. I'm guessing map isn't working for me here. Instead I am returning an array of the individual letters of the name "Jeff Adrien". Can someone help me understand where I am going wrong? //Code in question function playerStats(playerName) { let specificPlayer = allPlayers().players if (playerName === specificPlayer) { return specificPlayer } const stats = Object.values(playerName).map((nums) => { return [nums] }) return stats.flat() } console.log(playerStats('Jeff Adrien')) // details function allPlayers() { const everyPlayer = Object.assign(homePlayers, awayPlayers) return everyPlayer } const gameObject = () => { return { home: { teamName: 'Brooklyn Nets', colors: ['black', 'white'], players: { 'Alan Anderson': { number: 0, shoe: 16, points: 22, rebounds: 12, assists: 12, steals: 3, blocks: 1, slamDunks: 1, }, 'Reggie Evans': { number: 30, shoe: 14, points: 12, rebounds: 12, assists: 12, steals: 12, blocks: 12, slamDunks: 7, }, 'Brook Lopez': { number: 11, shoe: 17, points: 17, rebounds: 19, assists: 10, steals: 3, blocks: 1, slamDunks: 15, }, 'Mason Plumlee': { number: 1, shoe: 19, points: 26, rebounds: 12, assists: 6, steals: 3, blocks: 8, slamDunks: 5, }, 'Jason Terry': { number: 31, shoe: 15, points: 19, rebounds: 2, assists: 2, steals: 4, blocks: 11, slamDunks: 1, }, }, }, away: { teamName: 'Charlotte Hornets', colors: ['turquoise', 'purple'], players: { 'Jeff Adrien': { number: 4, shoe: 18, points: 10, rebounds: 1, assists: 1, steals: 2, blocks: 7, slamDunks: 2, }, 'Bismak Biyombo': { number: 0, shoe: 16, points: 12, rebounds: 4, assists: 7, steals: 7, blocks: 15, slamDunks: 10, }, 'DeSagna Diop': { number: 4, shoe: 14, points: 24, rebounds: 12, assists: 12, steals: 4, blocks: 5, slamDunks: 5, }, 'Ben Gordon': { number: 8, shoe: 15, points: 33, rebounds: 3, assists: 2, steals: 1, blocks: 1, slamDunks: 0, }, 'Brendan Haywood': { number: 33, shoe: 15, points: 6, rebounds: 12, assists: 12, steals: 22, blocks: 5, slamDunks: 12, }, }, }, } }
wow I was trying to work with an array and turn it into an object somehow.. it just clicked for me function playerStats(playerName) { for (const player in allPlayers()) { if (player === playerName) { return allPlayers()[player] } } }
Move y-Axis and hide in Chart.JS V3
So I've created this chart, and I'm trying to move the axis around so the Dark red is on the left, and the Blue and the sub of the bar charts is displayed, but I'm having trouble moving the dark red to the right side of this graf. I did try to use the position: 'right', but nothing changed. I'm trying to make it look like this: But I can only get this: var canvas = document.createElement('canvas'); div = document.getElementById('container'); canvas.id = "myChart"; canvas.style.zIndex = 8; div.appendChild(canvas); const labels = [ '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '1', '2', '3', '4', '5', '6', '7' ]; const data = { labels: labels, datasets: [{ label: 'Red', backgroundColor: 'rgb(255,0,0)', borderColor: 'rgb(255,0,0)', data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], stack: 'combined', type: 'bar', order: 4 }, { label: 'Yellow', backgroundColor: 'rgb(255,255,0)', borderColor: 'rgb(255,255,0)', data: [0, 2, 2, 0, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], stack: 'combined', type: 'bar', order: 4 }, { label: 'Orange', backgroundColor: 'rgb(255,159,64)', borderColor: 'rgb(255,159,64)', data: [9, 21, 21, 0, 21, 21, 21, 21, 18, 18, 18, 18, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9], stack: 'combined', type: 'bar', order: 4 }, { label: 'Grey light', backgroundColor: 'rgb(224,224,224)', borderColor: 'rgb(224,224,224)', data: [9, 20, 20, 0, 20, 20, 21, 21, 19, 19, 19, 19, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9], stack: 'combined', type: 'bar', order: 4 }, { label: 'Blue', backgroundColor: 'rgb(30,144,255)', borderColor: 'rgb(30,144,255)', data: [328, 421, 421, 0, 421, 421, 422, 422, 344, 344, 344, 344, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328], stack: 'combined_first', yAxisID: 'GreyDaBlue', order: 1 }, { label: 'Dark Red', backgroundColor: 'rgb(165,42,42)', borderColor: 'rgb(165,42,42)', data: [0.45, 1.55, 1.55, 0, 1.55, 1.55, 1.55, 1.55, 1.15, 1.15, 1.15, 1.15, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45], stack: 'combined_second', yAxisID: 'DarkR', order: 2 }, { label: 'Dark Grey', backgroundColor: 'rgb(80,80,80)', borderColor: 'rgb(80,80,80)', data: [18, 43, 43, 0, 43, 43, 44, 44, 38, 38, 38, 38, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18], stack: 'combined_third', yAxisID: 'GreyDaBlue', order: 3 } ] }; const config = { type: 'line', data: data, options: { plugins: { title: { display: true, text: 'WDC History Chart' }, zoom: { zoom: { wheel: { enabled: true, }, pinch: { enabled: true }, drag: { enabled: true }, mode: 'x', } } }, scales: { y: { stacked: true } } }, }; const myChart = new Chart( document.getElementById('myChart'), config ); <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <div id="container" class="wdc-canvas-size"> </div> So how do I move the Dark Red y-axis over to the right side?
I could use the DarkR id and position it right in scales like this var canvas = document.createElement('canvas'); div = document.getElementById('container'); canvas.id = "myChart"; canvas.style.zIndex = 8; div.appendChild(canvas); const labels = [ '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '1', '2', '3', '4', '5', '6', '7' ]; const data = { labels: labels, datasets: [{ label: 'Red', backgroundColor: 'rgb(255,0,0)', borderColor: 'rgb(255,0,0)', data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], stack: 'combined', type: 'bar', order: 4 }, { label: 'Yellow', backgroundColor: 'rgb(255,255,0)', borderColor: 'rgb(255,255,0)', data: [0, 2, 2, 0, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], stack: 'combined', type: 'bar', order: 4 }, { label: 'Orange', backgroundColor: 'rgb(255,159,64)', borderColor: 'rgb(255,159,64)', data: [9, 21, 21, 0, 21, 21, 21, 21, 18, 18, 18, 18, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9], stack: 'combined', type: 'bar', order: 4 }, { label: 'Grey light', backgroundColor: 'rgb(224,224,224)', borderColor: 'rgb(224,224,224)', data: [9, 20, 20, 0, 20, 20, 21, 21, 19, 19, 19, 19, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9], stack: 'combined', type: 'bar', order: 4 }, { label: 'Blue', backgroundColor: 'rgb(30,144,255)', borderColor: 'rgb(30,144,255)', data: [328, 421, 421, 0, 421, 421, 422, 422, 344, 344, 344, 344, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328, 328], stack: 'combined_first', yAxisID: 'GreyDaBlue', order: 1 }, { label: 'Dark Red', backgroundColor: 'rgb(165,42,42)', borderColor: 'rgb(165,42,42)', data: [0.45, 1.55, 1.55, 0, 1.55, 1.55, 1.55, 1.55, 1.15, 1.15, 1.15, 1.15, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45], stack: 'combined_second', yAxisID: 'DarkR', order: 2 }, { label: 'Dark Grey', backgroundColor: 'rgb(80,80,80)', borderColor: 'rgb(80,80,80)', data: [18, 43, 43, 0, 43, 43, 44, 44, 38, 38, 38, 38, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18], stack: 'combined_third', yAxisID: 'GreyDaBlue', order: 3 } ] }; const config = { type: 'line', data: data, options: { plugins: { title: { display: true, text: 'WDC History Chart' }, zoom: { zoom: { wheel: { enabled: true, }, pinch: { enabled: true }, drag: { enabled: true }, mode: 'x', } } }, scales: { y: { stacked: true }, DarkR: { position: 'right', // `axis` is determined by the position as `'y'` } } }, }; const myChart = new Chart( document.getElementById('myChart'), config ); <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <div id="container" class="wdc-canvas-size"> </div>
How to select random object from JSON file in discord.js
I've done some searching around and I found some posts on here but my code doesn't want to work. Basically I'm making a discord bot and I want to select an object from a JSON file at random. This is my command: const UserData = require('../data/users.js'); const monster = require('../data/monsters.json'); module.exports = { name: 'battle', aliases: ['fight'], cooldown: 0, description: 'User fights against a monster alone or in group', execute(client, message, args) { let enemy = monster[Math.floor(Math.random() * monster.length)] UserData.findOne({ userID: message.author.id }, (error, userdata) => { if (error) console.log(error); if (!userdata) { return message.reply(`you don't have an account!`); } else { console.log(enemy); return message.channel.send(`${enemy} spawned!`); } }) } } And this is my JSON file: "1" : { "name": "Blue Slime", "hp": 20, "atk": 12, "def": 10, "spatk": 3, "spdef": 12, "spd": 100, "gold": 10, "xp": 50, "lvl": 1 }, "2": { "name": "Red slime", "hp": 20, "atk": 12, "def": 10, "spatk": 3, "spdef": 12, "spd": 100, "gold": 10, "xp": 50, "lvl": 1 }, "3": { "name": "Green slime", "hp": 20, "atk": 12, "def": 10, "spatk": 3, "spdef": 12, "spd": 100, "gold": 10, "xp": 50, "lvl": 1 } } If I want put the objects in the command manually and then randomly select them it works and if instead of "monster.length" I put a number then it also works but I still get undefined if it should be 3. This way I also always get undefined in console log from monster.length. What am I doing wrong?
Your monsters.json file contains an object and objects don't have lengths. You can however convert it to an array, using Object.values() that returns an array of the given object's own enumerable property values. Check out the snippet below: let monsters = { 1: { name: 'Blue Slime', hp: 20, atk: 12, def: 10, spatk: 3, spdef: 12, spd: 100, gold: 10, xp: 50, lvl: 1, }, 2: { name: 'Red slime', hp: 20, atk: 12, def: 10, spatk: 3, spdef: 12, spd: 100, gold: 10, xp: 50, lvl: 1, }, 3: { name: 'Green slime', hp: 20, atk: 12, def: 10, spatk: 3, spdef: 12, spd: 100, gold: 10, xp: 50, lvl: 1, }, }; function randomObject(obj) { let arr = Object.values(obj); return arr[Math.floor(Math.random() * arr.length)]; } let enemy = randomObject(monsters); console.log(enemy);
Translating objects in nodeJS
I have the following object that I need to transform into a slightly different format. I'm pretty sure I can use nodes native map function to translate it. e.g. var animals = [ [ 'cats', 1726, 1143, 10, 105, 2382, 0 ], [ 'dogs', 640, 0, 0, 0, 0, 0 ], [ 'cows' 13509, 0, 3, 1, 196939, 19 ], [ 'sheep', 1573, 1084, 10, 105, 2266, 0 ] ] Into this format: new_animals = [{ name: 'cats', data: [1726, 1143, 10, 105, 2382, 0] }, { name: 'dogs', data: [640, 0, 0, 0, 0, 0] }, { name: 'cows', data: [13509, 0, 3, 1, 196939, 19] }, { name: 'sheep', data: [1573, 1084, 10, 105, 2266, 0] }]
Using ES2015 features: const newAnimals = animals.map(([name, ...data]) => ({ name, data })); Without ES2015 features: var newAnimals = animals.map(function (animal) { return { name: animal[0], data: animal.slice(1) }; });
its actually quite simple: var animals = [ ['cats', 1726, 1143, 10, 105, 2382, 0], ['dogs', 640, 0, 0, 0, 0, 0], ['cows', 13509, 0, 3, 1, 196939, 19], ['sheep', 1573, 1084, 10, 105, 2266, 0] ]; console.log(animals.map(([name, ...data]) => ({name, data}))); alternative if your node version does not support lambdas: animals.map(function (animal) { return { name: animal.shift(), data: animal }; });