1

I basicly need to do some replace logic to change the '.' to a '>' at here everything fine, i can just use the replace method from javascript, but i am searching the best way to do it.

Basicly i will use this function just for 1 specific task nothing more, i want to do this replace logic to my Name propertiy and description inside the object, so instead of doing a simple method that does the replace i need to pass it two times down.

At the moment i have this repeated: element.Name.replace('.', ' > ')

i created a method, but i thaught as the best possible way to maybe pass it to the function like: replaceMethod(firstProp,secondProp) where each prop gets replaced, so how can i inside the replace method just apply the same logic to all my arguments without using a useless for loop?

something like this:

replaceMethod(firstProp,secondProp) {
  allArgs.replace('.', ' > ')
}

i did this:

callerFunc() {
  // service get the object material, it has a name and description with '.'
  replaceMethod(material,material.Name,material.Description)

// do some logic after the method with the material }

    replaceMethod(material,...keys) {
              keys.forEach(k => material[k] = material[k].replace(/\./g, ' > '));
    }
4
  • 2
    please add a use case for the request. Commented Nov 11, 2017 at 15:05
  • my use case is that i just need to use the replace 2 times, 1 for the Name property and the other for the description, i just don't want to repeat the same logic two times Commented Nov 11, 2017 at 15:17
  • please add the data structure and what you have tried. Commented Nov 11, 2017 at 15:18
  • updated, see that repeated logic inside the replace method, i don't want that Commented Nov 11, 2017 at 15:25

2 Answers 2

1

In ES6, you could use rest parameters ... for collecting all arguments.

function replaceMethod(...keys)  {
    keys.forEach(k => object[k] = object[k].replace(/\./g, ' > '));
}

var object = { name: 'foo.bar.baz', town: 'st.peter' };

replaceMethod('name', 'town');

console.log(object);

ES5 with use of arguments object.

function replaceMethod()  {
    Array.prototype.forEach.call(arguments, function (k) {
        object[k] = object[k].replace(/\./g, ' > ');
    });
}

var object = { name: 'foo.bar.baz', town: 'st.peter' };

replaceMethod('name', 'town');

console.log(object);

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

5 Comments

yes, if oyu like to replace more than one dot. the dot in regex denotes one character and as dot, you need to escape it. the flag g denoted global replacemenet, not just the first one.
i pass the object at the begin of the function and then the spread, i get a undefined before the replace :S
please add the object to the question.
watch the changes i did above
you need to take the property only with the key names, not the values.
0

I would recommend passing the object as a parameter with the keys of the properties you want to change. Then return a new object with the changes instead of changing the object in place. This is a more functional approach without side effects. You can use the array reduce method. It is most convenient in ES6 using the spread operator ...

function replaceForKeys(obj, ...keys) {
  return keys.reduce(
    function (result, k) {
      return { ...result, [k]: obj[k].replace(/\./g, ' > ') };
    },
    Object.assign({}, obj)
  );
}
var obj = { 'foo': 'foo.bar', 'bar': 'bar.foo' };
var replaced = replaceForKeys(obj, 'foo', 'bar');

So the function takes every argument after the object as an array of keys and reduces over them returning the original object with the property replaced each time. The reduce method takes an initial value as the second parameter and in this case we use Object.assign to use a copy of the original object as the initial value. the [k]: syntax in the object is new in ES6 I believe and is a computed key. It just lets you assign keys in the object without knowing their value beforehand.

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.