/    Sign up×
Community /Pin to ProfileBookmark

How do I transform the content of this array?

Hi! How do I transform the first array of scores into the second one? Rather than having a list of score object, I’d like to get a list of user objects. Each user object must have username and difficultyLevels properties, with the latter grouping the scores by difficulty.

“`
const scoresList = [
{
scoredBy: {
username: ‘sleekcollins’
},
scoreValue: 14,
difficultyLevel: ‘easy’,
},
{
scoredBy: {
username: ‘sleekcollins’
},
scoreValue: 18,
difficultyLevel: ‘easy’,
},
{
scoredBy: {
username: ‘sleekcollins’
},
scoreValue: 24,
difficultyLevel: ‘hard’,
},
{
scoredBy: {
username: ‘aaronrodgers’
},
scoreValue: 32,
difficultyLevel: ‘medium’,
},
{
scoredBy: {
username: ‘patmahomes’
},
scoreValue: 20,
difficultyLevel: ‘expert’,
},
{
scoredBy: {
username: ‘patmahomes’
},
scoreValue: 17,
difficultyLevel: ‘easy’,
},
];

const scoresByUser = [
{
username: ‘sleekcollins’,
difficultyLevels: [
{
difficulty: ‘easy’,
scores: [14, 18]
},
{
difficulty: ‘medium’,
scores: []
},
{
difficulty: ‘hard’,
scores: [24]
},
{
difficulty: ‘expert’,
scores: []
}
]
},
{
username: ‘aaronrodgers’,
difficultyLevels: [
{
difficulty: ‘easy’,
scores: [0]
},
{
difficulty: ‘medium’,
scores: [32]
},
{
difficulty: ‘hard’,
scores: []
},
{
difficulty: ‘expert’,
scores: []
}
]
},
{
username: ‘patmahomes’,
difficultyLevels: [
{
difficulty: ‘easy’,
scores: [17]
},
{
difficulty: ‘medium’,
scores: []
},
{
difficulty: ‘hard’,
scores: []
},
{
difficulty: ‘expert’,
scores: [20]
}
]
},
];
“`

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJun 19.2020 — ``<i>
</i>&lt;script&gt;
"use strict";

function Obj(username){
this.username = username;
this.difficultyLevels = [];
this.difficultyLevels.push({difficulty: 'easy', scores:[]});
this.difficultyLevels.push({difficulty: 'medium', scores:[]});
this.difficultyLevels.push({difficulty: 'hard', scores:[]});
this.difficultyLevels.push({difficulty: 'expert', scores:[]});
}

const scoresList = [
{
scoredBy: {
username: 'sleekcollins'
},
scoreValue: 14,
difficultyLevel: 'easy',
},
{
scoredBy: {
username: 'sleekcollins'
},
scoreValue: 18,
difficultyLevel: 'easy',
},
{
scoredBy: {
username: 'sleekcollins'
},
scoreValue: 24,
difficultyLevel: 'hard',
},
{
scoredBy: {
username: 'aaronrodgers'
},
scoreValue: 32,
difficultyLevel: 'medium',
},
{
scoredBy: {
username: 'patmahomes'
},
scoreValue: 20,
difficultyLevel: 'expert',
},
{
scoredBy: {
username: 'patmahomes'
},
scoreValue: 17,
difficultyLevel: 'easy',
},
];
var username = [];
var scoresByUser = [];
var obj,obj1,obj2,name;
for(let i=0;i&lt;scoresList.length;i++){
obj1=scoresList[i];
name=obj1.scoredBy.username;
if(username.indexOf(name)==-1){
username.push(name);
scoresByUser.push(new Obj(name))
}
for(let i=0;i&lt;scoresByUser.length;i++){
obj = scoresByUser[i];
if(obj.username == name)break;
}
for(let i=0;i&lt;obj.difficultyLevels.length;i++){
obj2 = obj.difficultyLevels[i];
if(obj1.difficultyLevel == obj2.difficulty)break;
}
obj2.scores.push(obj1.scoreValue)
}
console.log(scoresByUser);
&lt;/script&gt;<i>
</i>
``
×

Success!

Help @easymoneysniper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...