简体   繁体   中英

How change the value of this.something with a looping

    if (this.name === '') {
        this.name= 'None';
    }
    if (this.obs === '') {
        this.obs = 'None';
    }

Guys, how can i dry this code? I am trying like this:

var v = [this.name, this.obs];

for(var i = 0; i < v.length; i++) {
   if(v[i] === '') {
       v[i] = 'None';
   } 
};

But is not working at all...

I think what you are looking for is a function. something like this

function setToNoneIfEmpty (stringInput) {
    if (stringInput === '') {
        stringInput = 'none';
    }
    return stringInput;
}

to make this cleaner you should make use of a ternary statement and just return the value and not reassign the parameter, so the function looks like this

function setToNoneIfEmpty (stringInput) {
    return stringinput === '' ? 'None' : stringinput;
}

with this you should be able to pass in this.name and this.obs and the function will change them accordingly. you would call them like this

this.name = setToNoneIfEmpty(this.name);
this.obs = setToNoneIfEmpty(this.obs);

Your original code that isn't working, isn't working because you created a multi-dimensional array and then didn't access the values as a multi-dimensional array.

The way that I propose will work a lot better and is easier to read


I am not a Pro at Javascript (By a longshot) but someone mentioned to me that it could be done differently, where you would actually access the object and pass in the object property that you want to check.

function setToNoneIfEmpty(obj, name) {
    if (obj[name] === '') { 
        obj[name] = 'None'; 
    } 
}

so instead of passing in a variable to return a string, we pass in an object and the property name, this makes the code even DRY-er as you can see when we put it to use

setToNoneIfEmpty(this, 'name')
setToNoneIfEmpty(this, 'obs')

Remember that we can access the properties of an object not only like object.property but also via the subscript operator: object['property'] . Therefore, we can loop over all names of the properties we want to change:

var propertyNames = ['name', 'obs'];

for(var i = 0; i < propertyNames.length; i++) {
    if(this[propertyNames[i]] === '') {
       this[propertyNames[i]] = 'None';
   } 
};

With Array.forEach , the code becomes a bit simpler:

['name', 'obs'].forEach(function (key) {
    if (this[key] === '') {
        this[key] = 'None';
    }
});

Use a for..in loop along with OR operator.

for(var key in this)
   this[key] = this[key] || 'None';

for..in loop loops through the enumerable properties of this and this[key] gets assigned to itself, if it isn't '' . If it is '' , then 'None' will be assigned to it, as '' is considered falsey

Try this:

var v = [this.name, this.obs];

v.forEach(function(elem){
    if(!elem) elem = 'NONE';
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM