Having trouble accessing the nested values of my object. JavaScript - 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]
}
}
}

Related

I want to calculate total sales

This is my data and I'd like to calculate total sales.
let order = [
{ id: 'A441', sales: [10, 12, 13, 10, 16, 22, 30] },
{ id: 'B234', sales: [2, 4, 3, 4, 2, 6, 8, 10] },
{ id: 'A617', sales: [5, 5, 5, 5, 5] },
{ id: 'C229', sales: [9, 7, 6, 8, 8, 10, 9, 3, 4, 5, 6] },
{ id: 'D412', sales: [25, 25, 23, 21] },
{ id: 'A054', sales: [2, 2, 3, 1, 5, 6, 7, 11, 2] },
{ id: 'B955', sales: [1, 1, 1, 1, 1, 1] },
{ id: 'M341', sales: [4, 5, 4, 5, 4] },
{ id: 'H103', sales: [3, 2, 2, 3, 1, 1] },
{ id: 'B199', sales: [6, 5, 4] },
{ id: 'D388', sales: [7, 8, 9, 8, 4, 4, 4, 3, 2, 1] }
];
If you want the total by sale type:
let orders = [
{ id: 'A441', sales: [10, 12, 13, 10, 16, 22, 30] },
{ id: 'B234', sales: [2, 4, 3, 4, 2, 6, 8, 10] },
{ id: 'A617', sales: [5, 5, 5, 5, 5] },
{ id: 'C229', sales: [9, 7, 6, 8, 8, 10, 9, 3, 4, 5, 6] },
{ id: 'D412', sales: [25, 25, 23, 21] },
{ id: 'A054', sales: [2, 2, 3, 1, 5, 6, 7, 11, 2] },
{ id: 'B955', sales: [1, 1, 1, 1, 1, 1] },
{ id: 'M341', sales: [4, 5, 4, 5, 4] },
{ id: 'H103', sales: [3, 2, 2, 3, 1, 1] },
{ id: 'B199', sales: [6, 5, 4] },
{ id: 'D388', sales: [7, 8, 9, 8, 4, 4, 4, 3, 2, 1] }
];
function getTotalSales(orders){
return orders.map(order=> ({...order, totalSales: order.sales.reduce((acc, cur)=> acc+=cur, 0)}))
}
console.log(getTotalSales(orders))
If you want the total of all sales:
let orders = [
{ id: 'A441', sales: [10, 12, 13, 10, 16, 22, 30] },
{ id: 'B234', sales: [2, 4, 3, 4, 2, 6, 8, 10] },
{ id: 'A617', sales: [5, 5, 5, 5, 5] },
{ id: 'C229', sales: [9, 7, 6, 8, 8, 10, 9, 3, 4, 5, 6] },
{ id: 'D412', sales: [25, 25, 23, 21] },
{ id: 'A054', sales: [2, 2, 3, 1, 5, 6, 7, 11, 2] },
{ id: 'B955', sales: [1, 1, 1, 1, 1, 1] },
{ id: 'M341', sales: [4, 5, 4, 5, 4] },
{ id: 'H103', sales: [3, 2, 2, 3, 1, 1] },
{ id: 'B199', sales: [6, 5, 4] },
{ id: 'D388', sales: [7, 8, 9, 8, 4, 4, 4, 3, 2, 1] }
];
function getTotalSales(orders){
return orders.map(({sales})=> sales).flat().reduce((acc, cur)=> acc+=cur, 0)
}
console.log(getTotalSales(orders))

How to group an array and make form to display in Chart?

I'm trying to create a array to display in a chart,
This are my data:
[
{ Cantidad: 2, Mesint: 4, DepartmentName: 'CALIDAD' },
{ Cantidad: 1, Mesint: 5, DepartmentName: 'CALIDAD' },
{ Cantidad: 2, Mesint: 3, DepartmentName: 'GERENCIA' },
{ Cantidad: 1, Mesint: 4, DepartmentName: 'MATERIALES' },
{ Cantidad: 1, Mesint: 6, DepartmentName: 'MATERIALES' },
{ Cantidad: 9, Mesint: 1, DepartmentName: 'PRODUCCION' },
{ Cantidad: 23, Mesint: 2, DepartmentName: 'PRODUCCION' },
{ Cantidad: 6, Mesint: 3, DepartmentName: 'PRODUCCION' },
{ Cantidad: 22, Mesint: 4, DepartmentName: 'PRODUCCION' },
{ Cantidad: 16, Mesint: 5, DepartmentName: 'PRODUCCION' },
{ Cantidad: 4, Mesint: 6, DepartmentName: 'PRODUCCION' },
{ Cantidad: 3, Mesint: 3, DepartmentName: 'PRUEBA ELECTRICA' },
{ Cantidad: 2, Mesint: 4, DepartmentName: 'PRUEBA ELECTRICA' },
{ Cantidad: 1, Mesint: 5, DepartmentName: 'PRUEBA ELECTRICA' },
{ Cantidad: 1, Mesint: 6, DepartmentName: 'PRUEBA ELECTRICA' },
{ Cantidad: 1, Mesint: 6, DepartmentName: 'TI' },
];
And need this group form:
Output
[
{ name: 'CALIDAD', data: [0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0] },
{ name: 'GERENCIA', data: [0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] },
{ name: 'MATERIALES', data: [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0] },
{ name: 'PRODUCCION', data: [9, 23, 6, 22, 16, 4, 0, 0, 0, 0, 0, 0] },
{ name: 'PRUEBA ELECTRICA', data: [0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0] },
{ name: 'TI', data: [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0] },
];
The array into previous group form is a array with "Cantidad" filter with Mesint, this a reference to mounths of year. So i need put 0 when the "head" (for example: "Calidad") doesnt have a value.
I'm trying group with map(), but i can't push the array with count (Value/"Cantidad") in month.
First, note that the expected output you have describe isn't valid, an Object should consist of key/value pairs. However, you can generate an object where the keys are the DepartmentName's and the values are the array of Cantidad for the 12 months:
{
"CALIDAD": [0,0,0,2,1,0,0,0,0,0,0,0],
"GERENCIA": [0,0,2,0,0,0,0,0,0,0,0,0],
"MATERIALES": [0,0,0,1,0,1,0,0,0,0,0,0],
"PRODUCCION": [9,23,6,22,16,4,0,0,0,0,0,0],
"PRUEBA ELECTRICA": [0,0,3,2,1,1,0,0,0,0,0,0],
"TI": [0,0,0,0,0,1,0,0,0,0,0,0]
}
That output will be similar to the one you expect and can be achieved using Array.reduce(), like shown on the next example:
const input = [
{Cantidad: 2, Mesint: 4, DepartmentName: "CALIDAD"},
{Cantidad: 1, Mesint: 5, DepartmentName: "CALIDAD"},
{Cantidad: 2, Mesint: 3, DepartmentName: "GERENCIA"},
{Cantidad: 1, Mesint: 4, DepartmentName: "MATERIALES"},
{Cantidad: 1, Mesint: 6, DepartmentName: "MATERIALES"},
{Cantidad: 9, Mesint: 1, DepartmentName: "PRODUCCION"},
{Cantidad: 23, Mesint: 2, DepartmentName: "PRODUCCION"},
{Cantidad: 6, Mesint: 3, DepartmentName: "PRODUCCION"},
{Cantidad: 22, Mesint: 4, DepartmentName: "PRODUCCION"},
{Cantidad: 16, Mesint: 5, DepartmentName: "PRODUCCION"},
{Cantidad: 4, Mesint: 6, DepartmentName: "PRODUCCION"},
{Cantidad: 3, Mesint: 3, DepartmentName: "PRUEBA ELECTRICA"},
{Cantidad: 2, Mesint: 4, DepartmentName: "PRUEBA ELECTRICA"},
{Cantidad: 1, Mesint: 5, DepartmentName: "PRUEBA ELECTRICA"},
{Cantidad: 1, Mesint: 6, DepartmentName: "PRUEBA ELECTRICA"},
{Cantidad: 1, Mesint: 6, DepartmentName: "TI"}
];
let res = input.reduce((acc, {Cantidad, Mesint, DepartmentName}) =>
{
acc[DepartmentName] = acc[DepartmentName] || [0,0,0,0,0,0,0,0,0,0,0,0];
acc[DepartmentName][Mesint - 1] = Cantidad;
return acc;
}, {});
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
If the generated output don't meet your goal, you can use the example as a guide and adapt it to your needs. For example, if you want something like next:
[
{name: "CALIDAD", data: [0,0,0,2,1,0,0,0,0,0,0,0]},
{name: "GERENCIA", data: [0,0,2,0,0,0,0,0,0,0,0,0]},
{name: "MATERIALES", data: [0,0,0,1,0,1,0,0,0,0,0,0]},
{name: "PRODUCCION", data: [9,23,6,22,16,4,0,0,0,0,0,0]},
{name: "PRUEBA ELECTRICA", data: [0,0,3,2,1,1,0,0,0,0,0,0]},
{name: "TI", data: [0,0,0,0,0,1,0,0,0,0,0,0]}
]
Then, you can use next code after the .reduce() method
res = Object.entries(res).map(([name, data]) => ({name, data}));

PlotlyJS - Set length of category axis in Surface Plot

I am trying to manually set the length of a category axis. In detail I want the length of the x axis equal to the length of the y axis. So far I tried diverse layout settings from plotly manual without results.
var layout = {
width: 400,
height: 300,
plot_bgcolor: '#98ff6d',
};
var data = [{
z: [
[1, 20, 10, 12, 1, 1, 20, 10, 12, 1],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 20, 20, 12, 0],
[1, 20, 10, 12, 1, 13, 20, 0, 12, 21],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 20, 10, 1, 12],
[1, 20, 10, 12, 1, 1, 20, 10, 12, 14],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 13, 4, 12, 16, 1, 21, 20, 0, 18],
[1, 20, 0, 12, 1, 21, 65, 10, 12, 1],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 20, 10, 15, 0],
[1, 20, 10, 12, 1, 1, 30, 10, 12, 14],
[24, 13, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 29, 1, 19, 31],
[1, 20, 10, 12, 1, 31, 0, 10, 12, 19],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 20, 10, 11, 21],
[1, 0, 10, 12, 1, 1, 10, 10, 12, 21],
[24, 1, 0, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 20, 1, 19, 36],
[1, 20, 10, 12, 1, 21, 21, 10, 12, 31],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 12, 4, 12, 16, 1, 21, 43, 12, 18],
[1, 20, 10, 12, 1, 1, 39, 10, 12, 13],
[24, 1, 12, 19, 0, 1, 20, 12, 2, 1],
[8, 1, 4, 12, 16, 1, 20, 10, 1, 11],
[1, 20, 10, 12, 1, 19, 38, 10, 12, 16],
[24, 12, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 1, 4, 12, 16, 1, 29, 4, 12, 3],
[1, 20, 0, 12, 1, 1, 23, 10, 12, 11],
[24, 1, 12, 19, 0, 1, 20, 10, 12, 1],
[8, 19, 4, 12, 16, 1, 20, 10, 12, 19],
[1, 20, 10, 12, 1, 1, 24, 10, 12, 0],
[24, 1, 12, 19, 023, 1, 22, 10, 12, 1],
[8, 1, 41, 12, 16, 1, 20, 10, 12, 11]
],
x: ['T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10'],
y: ['Messung 1', 'Messung 2', 'Messung 3',
'Messung 4', 'Messung 5', 'Messung 6',
'Messung 7', 'Messung 8', 'Messung 9',
'Messung 10', 'Messung 11', 'Messung 12',
'Messung 13', 'Messung 14', 'Messung 15',
'Messung 16', 'Messung 17', 'Messung 18',
'Messung 19', 'Messung 20', 'Messung 21',
'Messung 22', 'Messung 23', 'Messung 24',
'Messung 25', 'Messung 26', 'Messung 27',
'Messung 28', 'Messung 29', 'Messung 30',
'Messung 31', 'Messung 32', 'Messung 33',
'Messung 34', 'Messung 35', 'Messung 36'
],
type: 'surface'
}];
Plotly.newPlot('plot', data, layout, {
displayModeBar: true,
displaylogo: false
});
#plot {
height: 100vh, width:100vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified plotly.js JavaScript -->
<script type="text/javascript" src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="plot"></div>
Give your layout Object a scene, and set your desired range on its axis, yaxis, and (if you like) zaxis properties. E.g.
var MIN_VAL = -100, MAX_VAL = 100;
var layout = {
scene:{
axis: {
nticks: 10,
range: [ MIN_VAL, MAX_VAL ]
},
yaxis: {
nticks: 10,
range: [ MIN_VAL, MAX_VAL ]
},
zaxis: {
nticks: 7,
range: [ MIN_VAL, MAX_VAL ]
},
aspectmode: "manual",
aspectratio: { x: 1, y: 1, z: 0.7 },
bgcolor : '#98ff6d'
},
autosize: false,
width: 400,
height: 300,
margin: { l: 0, r: 0, b: 50, t: 50, pad: 4 }
};
Plotly.newPlot('plot', data, layout);
Plotly has a working example (that shows the use of even more layout options) on CodePen.

Toggle visibility of (plotly.js - scatter) y-axis & traces

New to plotly.js
(I am in angular environment)
I wanna put the traces inside of a multi-select drop menu. Same for all the y axis. To be able to toggle the visibility of these. Suggestions...the easy/right way.
I tried to affect the svg containers with css but no effect:
For axis:
var update = {
'yaxis.visible': false
};
Plotly.relayout(myDiv, update)
For traces:
data = [
{
line: {width: 5},
name: "myName1",
type: "scatter",
visible: "legendonly", // Plot trace but hide it to the user
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [10, 11, 2, 23, 14, 5, 16, 7, 18, 29, 10]
},
{
line: {width: 5},
name: "myName2",
type: "scatter",
visible: "legendonly" ,// Plot trace but hide it to the user
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [10, 1, 12, 13, 4, 25, 16, 17, 8, 19, 20]
}
];
allTraces=document.getElementById("myDiv").data; // Get data from chart AFTER plotting it.
// Find index of trace-object with "name" property = "text to find":
result = allTraces.findIndex(obj => {
return obj.name === "text to find";
}
// Make specified trace visible to user:
Plotly.restyle(document.getElementById("myDiv"), {"visible": true}, [result]);

Google LineChart legend and gridlines

I'd like to get this Google LineChart to look a little more like this chart from GitHub. I'm recreating GitHub's chart because I want a version of it in high quality (vector) and not a highly compressed JPEG (who in their right mind saves charts as JPEGs anyway!).
I'd like to move the legend into the graph and add vertical gridlines (on the horizontal axis "Year"). Does anyone know how to accomplish this?
Here's the code, but please feel free to edit my JSFiddle:
google.load('visualization', '1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(function() {
var data = google.visualization.arrayToDataTable([
['Year', 'JavaScript', 'Java', 'Ruby', 'PHP', 'Python', 'CSS', 'C++', 'Objective C', 'C', 'C#', 'Perl', 'Emacs Lisp', 'VimL', 'Shell','HTML'],
['2008', 2, 7, 1, 4, 3, null, 8, 9, 5, null, 6, null, null, null, null],
['2009', 2, 7, 1, 4, 3, null, 8, 9, 6, null, 5, 10, null, null, null],
['2010', 2, 5, 1, 4, 3, null, 8, 9, 6, null, 7, null, 10, null, null],
['2011', 2, 5, 1, 4, 3, null, 7, 8, 6, 10, 9, null, null, null, null],
['2012', 2, 3, 1, 4, 5, null, 7, 8, 6, 9, null, null, null, 10, null],
['2013', 1, 3, 2, 4, 5, 10, 7, 8, 6, 9, null, null, null, null, null],
['2014', 1, 2, 3, 4, 5, 6, 7, 9, 8, 10, null, null, null, null, null],
['2015', 1, 2, 3, 4, 5, 6, 7, null, 9, 8, null, null, null, null, 10]
]);
var options = {
title: 'Rank of top languages on GitHub over time',
width: '100%',
height: 600,
hAxis: {
title: 'Time',
},
vAxis: {
title: 'Rank',
viewWindow: {
min: 1
},
direction: -1,
gridlines: {
count: 11
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
});
Change data type -> type: 'date'
google.load('visualization', '1.1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(function() {
var data = google.visualization.arrayToDataTable([
[{label: 'Year', type: 'date'}, 'JavaScript', 'Java', 'Ruby', 'PHP', 'Python', 'CSS', 'C++', 'Objective C', 'C', 'C#', 'Perl', 'Emacs Lisp', 'VimL', 'Shell','HTML'],
[new Date(2008, 0, 0), 2, 7, 1, 4, 3, null, 8, 9, 5, null, 6, null, null, null, null],
[new Date(2009, 0, 0), 2, 7, 1, 4, 3, null, 8, 9, 6, null, 5, 10, null, null, null],
[new Date(2010, 0, 0), 2, 5, 1, 4, 3, null, 8, 9, 6, null, 7, null, 10, null, null],
[new Date(2011, 0, 0), 2, 5, 1, 4, 3, null, 7, 8, 6, 10, 9, null, null, null, null],
[new Date(2012, 0, 0), 2, 3, 1, 4, 5, null, 7, 8, 6, 9, null, null, null, 10, null],
[new Date(2013, 0, 0), 1, 3, 2, 4, 5, 10, 7, 8, 6, 9, null, null, null, null, null],
[new Date(2014, 0, 0), 1, 2, 3, 4, 5, 6, 7, 9, 8, 10, null, null, null, null, null],
[new Date(2015, 0, 0), 1, 2, 3, 4, 5, 6, 7, null, 9, 8, null, null, null, null, 10]
]);
var options = {
title: 'Rank of top languages on GitHub over time',
width: '100%',
height: 800,
hAxis: {
title: 'Time',
gridlines: {
count: -1
},
},
vAxis: {
title: 'Rank',
viewWindow: {
min: 1
},
direction: -1,
gridlines: {
count: -1
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
});
https://jsfiddle.net/mblenton/x00omtxL/15/

Categories