• 0

Transform json to json

Problem description :

Write a function which transforms the given JSON as shown belows :

Input : Given an endorsment array with objects containing skill and user keys

var endorsements = [
  { skill: 'javascript', user: 'Chad' },
  { skill: 'javascript', user: 'Bill' },
  { skill: 'javascript', user: 'Sue' },
  { skill: 'html', user: 'Sue HTML' },
  { skill: 'css', user: 'Sue CSS' },
  { skill: 'css', user: 'Bill CSS' }
];

Output : And we want to transform an given endorsment array with an array which is a collection of objects with key skill and users. But user key is an array containing all the users with that particular skill.

[
  { skill: 'javascript', user: [ 'Chad', 'Bill', 'Sue' ], count: 3 },
  { skill: 'css', user: [ 'Sue', 'Bill' ], count: 2 },
  { skill: 'html', user: [ 'Sue' ], count: 1 } 
];

Logic :

  • Create a JSON with key being skill value, for example javascript
  • Add value to the key being user with that skill, for example Chad
  • Map the newly created JSON to format accordingly output

Solution :