Calculate percentile rank using javascript for each user in array of json objects based on user's overall place - javascript

Using javascript, how can I calculate the percentile rank for each participant in a results list where person with place = 1 (Joe) has a 100% rank since they won a race, for instance, but then all other people's ranks are lower from there? Here are some sample results. Can someone please help?
const results = [{
name: 'joe',
place: 1,
rank: '100%'
}, {
name: 'roger',
place: 2,
rank: '?'
}, {
name: 'heather',
place: 3,
rank: '?'
}, {
name: 'craig',
place: 4,
rank: '?'
}, {
name: 'sally',
place: 5,
rank: '?'
}, {
name: 'cory',
place: 6,
rank: '?'
}, {
name: 'joel',
place: 7,
rank: '?'
}];

You can subtract one less than each person's rank from the number of people and divide by the number of people to get the percentage rank.
const results = [{
name: 'joe',
place: 1,
rank: '?'
}, {
name: 'roger',
place: 2,
rank: '?'
}, {
name: 'heather',
place: 3,
rank: '?'
}, {
name: 'craig',
place: 4,
rank: '?'
}, {
name: 'sally',
place: 5,
rank: '?'
}, {
name: 'cory',
place: 6,
rank: '?'
}, {
name: 'joel',
place: 7,
rank: '?'
}];
results
.forEach(person=>person.rank
= (+((results.length - person.place + 1) / results.length * 100).toFixed(2)) + '%');
console.log(results);

Related

How to enumare objects and total of them with the same key with another object in array js

I'm populing a pdf template with an array of employees, and now I need to count the number of employees working in a same department, I found a way to count the total of concurrences but I can't enumarate the employee working in the department and the total of them. Can you help me? Thanks!!!
For example, I have this array of objects
const employees = [
{
id: 1,
name: "john",
department: {
id: 1,
},
},
{
id: 1,
name: "Mike",
department: {
id: 3,
},
},
{
id: 1,
name: "Leona",
department: {
id: 1,
},
},
{
id: 1,
name: "Lara",
department: {
id: 1,
},
},
];
Result Expected:
const employees = [
{
id: 1,
name: "john",
department: {
id: 1,
},
totalForDeparments: "1/3",
},
{
id: 1,
name: "Mike",
department: {
id: 3,
},
totalForDeparments: "1/1",
},
{
id: 1,
name: "Leona",
department: {
id: 1,
},
totalForDeparments: "2/3",
},
{
id: 1,
name: "Lara",
department: {
id: 1,
},
totalForDeparments: "3/3",
},
];
First you group by the department.id - then you can iterate the original array adding the correct indexes.
const employees = [{id:1,name:"john",department:{id:1}},{id:1,name:"Mike",department:{id:3}},{id:1,name:"Leona",department:{id:1}},{id:1,name:"Lara",department:{id:1}},];
var grouped = employees.reduce(function(agg, item) {
agg[item.department.id] = agg[item.department.id] || {
count: 0,
current: 0
}
agg[item.department.id].count++;
return agg;
}, {});
employees.map(function(item) {
var data = grouped[item.department.id]
data.current++;
item.totalForDeparments = data.current + "/" + data.count
})
console.log(employees)
.as-console-wrapper {max-height: 100% !important}

How to duplicate an object in an array by given quantity, ES6 and above

I'm trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
// desired return
[
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 2, name: "Hat", price: 6.5}
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
]
My code:
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects= [];
Object.entries(objects).forEach(([key, value]) => {
for (let i=0; i < value.quantity; i++){
newObjects.push({ id: value.id, name: value.name, price: value.price})
}
});
console.log(newObjects);
So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?
You could use .fill() and .flatMap().
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects = objects.flatMap(e=>
Array(e.quantity).fill({id: e.id, name: e.name, price: e.price})
);
console.log(newObjects);
You can use an array reduce along with an array fill.
The map is required only if you want to have unique references otherwise you can fill using the same object.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
const output = objects.reduce((a, c) => {
return a.concat(Array(c.quantity).fill({}).map(x=>({
id: c.id,
name: c.name,
price: c.price
})))
}, []);
console.log(output)

Parse js table from the file

I write a programe in JavaScript where I want to add file "tables.js". There are many tables saved in this file.I want to validate the data in each table.
How can I save each of these tables as a separate variable? var people = ...; var city = ...
Part of tables.js file below.
{
people: [{
id: 1,
name: 'Bob',
lastName: 'Asdfg'
}, {
id: 2,
name: 'Carl',
lastName: 'Qwerty'
}],
city: [{
id: 1,
name: 'Prague',
size: 'M',
continent:'Europe'
}, {
id: 1,
name: 'London',
size: 'XL',
continent:'Europe'
}]
}
I have tried JSON.parse so far but unfortunately I can't split this file into separate tables.
What you have to do is extract from the object Keys and allocate them to new variables
There are two ways of doing this . One is dot Notations as per the example and the other is bracket notation which looks like this
let people = data['people'];
let city= data['city'];
var data = {
people: [{
id: 1,
name: 'Bob',
lastName: 'Asdfg'
}, {
id: 2,
name: 'Carl',
lastName: 'Qwerty'
}],
city: [{
id: 1,
name: 'Prague',
size: 'M',
continent:'Europe'
}, {
id: 1,
name: 'London',
size: 'XL',
continent:'Europe'
}]
};
let people = data.people;
let city = data.city;
console.log(people)
console.log('=================')
console.log(city)
Same as above but with ES6 (latest JS version) constants and deconstruct features.
const data = {
people: [{
id: 1,
name: 'Bob',
lastName: 'Asdfg'
}, {
id: 2,
name: 'Carl',
lastName: 'Qwerty'
}],
city: [{
id: 1,
name: 'Prague',
size: 'M',
continent: 'Europe'
}, {
id: 1,
name: 'London',
size: 'XL',
continent: 'Europe'
}]
}
const {people, city} = data
console.log('People:', people)
console.log('City:', city)

Get value from a JavaScript array.object using id number

Based on the var data below. How can I get the full_name value if all I have is the number 2? Number 2 corresponds with id: 2 and the full_name would be Eric
var data = [{
id: 0,
full_name: 'None',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 1,
full_name: 'John',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 2,
full_name: 'Eric',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 3,
full_name: 'Larry',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 4,
full_name: 'Rick',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 5,
full_name: 'John',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 6,
full_name: 'Eric',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 7,
full_name: 'Larry',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 8,
full_name: 'Rick',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 9,
full_name: 'John',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 10,
full_name: 'Eric',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 11,
full_name: 'Larry',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}, {
id: 12,
full_name: 'Rick',
gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
}];
(data.filter(function(x) { return x.id == 2 })[0] || {}).full_name;
// => "Eric"
data.filter will return only elements that fit the given criterion; in this case, id being 2. Since we only care about one element, we will just take the first found element; but if none exist, .full_name will give an error about full_name not being defined on undefined, so we put in an empty object just in case the search fails using || {}.
Amadan's answer most probably is better/more failure proof than mine, but if you know that your array of objects will always contain objects from Id 0 to Id infinite, you could also get it with
var wanted = data[number].fullName
ES6 offers Array#find, to find the first element in an array that meets some condition:
var wanted = data.find(elt => elt.id === 2);
If you don't have this available, there are various polyfills, including one on the page referenced above.
function myQuery(array, id){
for(i in array){
if(array[i].id === id){
console.log(data[i].full_name);
return data[i].full_name;
}
}
}
myQuery(data, 2)
If you are keeping your ID equal to the place in the array, you could just do this.
function myQuery(id){
return array[id].full_name
}

Expand/Collapse all the gof kendoTreeList

I am using kendoTreeList
and I am trying to expand all the groups. Here is my code sample
But it seems like the kendoTreeList support only expanding the first group. I tried the following selector in the expand method as well.
treeList.expand($(".k-treelist-group")); to expand all the groups. Even though the selector $(".k-treelist-group").length is 3 (total number of groups) but the treelist only expand the first group.
Any suggestion please let me know.
You are right, according with the information on their site it expands the row and not the rows.
Then you can iterate for getting the same effect:
var treeList = $("#treeList").data("kendoTreeList");
var rows = $("tr.k-treelist-group", treeList.tbody);
$.each(rows, function(idx, row) {
treeList.expand(row);
});
$(document).ready(function() {
$("#treeList").kendoTreeList({
columns: [ "id", "name" ],
loadOnDemand:false,
dataSource: [
{ id: 1, parentId: null, name: "Group", age: 30 },
{ id: 2, parentId: 1, name: "John Doe", age: 33 },
{ id: 3, parentId: 1, name: "Johson", age: 33 },
{ id: 4, parentId: null, name: "Group 2", age: 30 },
{ id: 5, parentId: 4, name: "Doe ", age: 33 },
{ id: 6, parentId: 4, name: "Noomi", age: 33 },
{ id: 7, parentId: null, name: "Group 3", age: 30 },
{ id:8, parentId: 7, name: "Doe ", age: 33 },
{ id: 9, parentId: 7, name: "Noomi", age: 33 }
]
});
var treeList = $("#treeList").data("kendoTreeList");
var rows = $("tr.k-treelist-group", treeList.tbody);
$.each(rows, function(idx, row) {
treeList.expand(row);
});
});
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js"></script>
<div id="treeList"></div>
If you expand many rows you can get into some performance issues, then one alternative to looping the .expand method is to modify the data and bind it again.
var treeList = $("#treeList").data("kendoTreeList");
var dataItems = treeList.dataSource.data();
$.each(dataItems, function(i, item) {
item.expanded = true;
});
treeList.dataSource.data(dataItems);
You can also modify the data before it's bound.
dataSource: {
data: [
{ id: 1, parentId: null, name: "Group", age: 30 },
{ id: 2, parentId: 1, name: "John Doe", age: 33 },
{ id: 3, parentId: 1, name: "Johson", age: 33 },
{ id: 4, parentId: null, name: "Group 2", age: 30 },
{ id: 5, parentId: 4, name: "Doe ", age: 33 },
{ id: 6, parentId: 4, name: "Noomi", age: 33 },
{ id: 7, parentId: null, name: "Group 3", age: 30 },
{ id:8, parentId: 7, name: "Doe ", age: 33 },
{ id: 9, parentId: 7, name: "Noomi", age: 33 }
],
schema: {
parse: function(data) {
$.each(data, function(i, item) {
item.expanded = true;
});
return data;
}
}
}

Categories

Resources