Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Forum » x-Forum » Question

Array content transformation-CppBuzz-Forum

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]
      }
    ]
  },
];

Posted by easymoneysniper 2020-06-19 05:23:58

No Ans Posted

Please login to comment

Log In


Ask New Question