Traverse all properties of a VueJS object

2

I'm using VueJS in a project and I need to go through all the properties of the object associated with v-model .

I know that if it were an array I could use u for , foreach , map and many other ways to go through it.

But what I need to do is to: Traverse all properties of an object and make a replace() only on properties containing / , - , . , ( , ) , because those characters come from the masks that were used in input 's, and I do not want to save those characters in the database.

If I were to use the masquerade library as a component I would be able to get the value out without the characters in question, but I'm using the masquerade library as a v-mask directive, and the value that comes from input is always with the mask.

I have researched a lot but I have not found an efficient method to solve this problem, if anyone can help I thank you ...

Note: It is a requirement of the project that input are not the v-mask being used as a component, since they were customized and it is a requirement to use them

    
asked by anonymous 02.10.2017 / 14:20

1 answer

2

You have two options:

  • change the object internally
  • create a new object and override the old one

In your case it makes no difference because the key values are strings. But if they were other objects the first approach could be problematic because it would change objects that could have references in other parts of the code.

In your case you can do this:

obj = Object.keys(obj).reduce((novoObj, key) => {
    novoObj[key] = obj[key].replace(/[\.\-]/g, '');
    return novoObj;
}, {});
    
02.10.2017 / 15:15