0
var result = [{"figure figure watch?v="}, {"figure data watch?v="}]
console.log(result.replace(res=> res.('figure', 'ifigure', 'watch?v=' , 'embeded/')))

I want result = [{"ifigure ifigure embeded/"}, {"ifigure data embeded/"}]

Can you help?

5
  • You can use replace of JavaScript like str.replace("figure figure watch?v=", "ifigure ifigure embeded/"); Commented Mar 1, 2021 at 6:31
  • yea, but the data will be fetched form database. So I am not sure always figure will be availabe so I need if availabe then replace all figure to ifigure and watch?v= to embeded/ Commented Mar 1, 2021 at 6:34
  • 1
    [{"figure figure watch?v="}, {"figure data watch?v="}] gives a syntax error. What actual input do you have? Is it [ "figure figure watch?v=", "figure data watch?v=" ] ? Commented Mar 1, 2021 at 6:36
  • Yes [ "figure figure watch?v=", "figure data watch?v=" ] this Commented Mar 1, 2021 at 6:38
  • You need to check if data is available like if(res){ //do here logic} Commented Mar 1, 2021 at 6:39

2 Answers 2

1

The simplest way will be to use Array.map method with regex -

var result = ["figure figure watch?v=", "figure data watch?v="];
result.map(e => e.replace(/watch\?v=/g, "embeded/").replace(/figure/g, "ifigure"));
// (2) ["ifigure ifigure embeded/", "ifigure data embeded/"]

Check out this question for more on replacing with Regex Fastest method to replace all instances of a character in a string

Sign up to request clarification or add additional context in comments.

Comments

1

First of all [{"ifigure ifigure embeded/"}, {"ifigure data embeded/"}] you have a syntax error here. you cannot enclose string in an object. you will need to do something like this. if in future you want to replace new string, just add them in this object.

var mapObj = {
   'watch?v=':"embeded/",
   'figure':"ifigure"
};

var result = ["figure figure watch?v=", "figure data watch?v="];
console.log(result.map(res=> res.replace(/watch\?v=|figure/gi, function(matched){
  return mapObj[matched];
})));

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.