How to complete such a task?

  • 0
    const person = {
        id: 29,
        name: "John",
        role: "Admin",
        salary: 999
    };
    const templatesSet = [
        "/items/%id%/%name%",
        "/items/%id%/%role%",
        "/items/%id%/%salary%"
    ];
    
    const pathes = pemplatesSet.map(pathTemplate => {
        return path(user, pathTemplate);
    });
    
    function path(obj, template) {
    
        let result = "";
         return result;
    }

    There is such a template, you need to bring it to this
    ["/ items / 29 / John", "/ items / 29 / Admin", "/ items / 29/999"]

    Tried split / join / replace - but not that.
    I tried to iterate over in the path function, then it displays the object in the object and 3 pieces, this can be seen from the brute force in pathes, I will be grateful for the help
    JavaScript Anonymous, Oct 8, 2020

  • 1 Answers
  • 0
    const person = {
    id: 29,
    name: "John",
    role: "Admin",
    salary: 999
    };
    const templatesSet = [
    "/items/%id%/%name%",
    "/items/%id%/%role%",
    "/items/%id%/%salary%"
    ];

    const pathes = templatesSet.map(t => t.replace(/%(.*?)%/g, (s,k)=>person[k]));

    console.log(pathes); // [ "/items/29/John", "/items/29/Admin", "/items/29/999" ]
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!