1

I need to know how to replace the urltext with an object using javavscript.

If the url is www.xyz.com/en/all-services-from-mal-to-sin/details?amount=1000&scy=SGD and if the lang is en, then replace the url with matching object key and if the lang is zh then replace the url with the matching object value.

ExpectedOutput:
if url is 
www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD 
=> output :www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD

if url is 
www.xyz.com/zh/all-services-from-mal-to-sin?amount=1000&scy=SGD  
=> output: www.xyz.com/zh/hui-zhi-phi-tho-zmal-zhi-stin?amount=1000&scy=SG

if url is 
www.xyz.com/en/hui-zhi-phi-tho-zmal-zhi-stin?amount=1000&scy=SG 
 => output: www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD
var obj1={
    "transfer-services": "xi-hou-zhi-n",
    "about-info": "zhi-zhu",
    "contact": "zhi-phi",
    "all-services-from": "hui-zhi-phi-tho",
    "to": "zhi",
    "sin": "stin",
    "mal": "zmal"
};

function transformURL(url,value) {
    let [base, lang, segment, ...rest] = url.split('/');
    lang=value;
    if(obj1.hasOwnProperty(segment)) {
        segment = obj1[segment];
    } else {
        Object.entries(obj1).forEach(([key, val]) => {
            if(val == segment) {segment = key};
        });
    }

    return [base, lang, segment, ...rest].join('/');
}
console.log(transformURL('www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD', "zh"));
4
  • What should be your output url? Commented Jul 12, 2019 at 6:25
  • @RAVIPATEL thanks for reply, mentioned in Expected Output Commented Jul 12, 2019 at 6:30
  • It's so specific, I would just use if conditionals or a switch. You're making this harder than it needs to be. String.replace(). Commented Jul 12, 2019 at 6:32
  • @StackSlave thanks for reply, apologies, updated code Commented Jul 12, 2019 at 6:33

2 Answers 2

1

The mapping you have is en to zh, if you want 2 way conversion you need the reverse mapping too.

And your check obj1.hasOwnProperty(segment) and val === segment both of them are never going to work as all the keys in your object are partial, your segment is a combination of multiple keys, so you need to loop through the keys, check if key is part of the segment and replace that part of the segment.

const enToZh = {
    "transfer-services": "xi-hou-zhi-n",
    "about-info": "zhi-zhu",
    "contact": "zhi-phi",
    "all-services-from": "hui-zhi-phi-tho",
    "to": "zhi",
    "sin": "stin",
    "mal": "zmal"
};

const zhToEn = Object.keys(enToZh).reduce((a, c) => (
    { ...a, [enToZh[c]]: c }
), {});

function transformURL(url) {
    let [base, lang, segment, ...rest] = url.split('/');
    const obj = lang === 'en' ? zhToEn : enToZh;
    Object.keys(obj).forEach(key => {
        segment = segment.replace(key, obj[key]);
    });

    return [base, lang, segment, ...rest].join('/');
}

console.log(transformURL('www.xyz.com/zh/all-services-from-mal-to-sin/details?amount=1000&scy=SGD'));
Sign up to request clarification or add additional context in comments.

1 Comment

it works for two languages but doesnot work for multi language scenario, can you please please help, got stuck in code. stackoverflow.com/questions/57138754/…
0

This might help you.

function transformURL(url,value) {
let [base, lang, segment, ...rest] = url.split('/');
lang=value;

var newsegment=segment.split('?')[0]

   if(lang=="zh")
   {
       newsegment = obj1["all-services-from"]+"-"+obj1["mal"]+"-"+obj1["to"]+"-"+obj1["sin"]+"?"+segment.split('?')[1];
   }else{
      newsegment = "all-services-from"+"-"+"mal"+"-"+"to"+"-"+"sin"+"?"+segment.split('?')[1];
   }

   return [base, lang, newsegment, ...rest].join('/');
}

https://jsfiddle.net/d2tq4w3r/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.