Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const user = {
  2. id: 20,
  3. name: "John Dow",
  4. role: "QA",
  5. salary: 100
  6. }
  7.  
  8. const apiTemplatesSet1 = [
  9. "/api/items/%id%/%name%",
  10. "/api/items/%id%/%role%",
  11. "/api/items/%id%/%salary%"
  12. ];
  13.  
  14. const apiPathes = apiTemplatesSet1.map(apiPathTemplate => {
  15.   return getApiPath(user, apiPathTemplate);
  16. });
  17.  
  18.  
  19.  
  20. function getApiPath(obj, template) {
  21.   let result = template
  22.   template.match(/%(.*?)%/g).map(function(val){
  23.       let currValue = val.replace(/%/g, '');
  24.       result = result.replace(val, obj[currValue])
  25.   });
  26. return result;
  27. }
  28.  
  29. console.log(encodeURI(apiPathes));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement