简体   繁体   English

如何使用循环更改this.something的值

[英]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. 有了这个你应该能够传入this.namethis.obs ,该函数将相应地更改它们。 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. 我不是Java专业人士(长话短说),但是有人向我提到可以用不同的方式完成,实际上您可以在其中访问对象并传递要检查的对象属性。

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 因此,我们不是传入一个变量来返回一个字符串,而是传入一个对象和属性名称,这使得代码甚至是DRY-er,就像我们在使用它时所看到的那样

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'] . 请记住,我们不仅可以像object.property一样访问对象的属性,还可以通过下标操作符访问object['property']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: 使用Array.forEach ,代码变得更简单:

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

Use a for..in loop along with OR operator. 使用for..in循环和OR运算符。

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 '' . for..in循环遍历的枚举的属性thisthis[key]被分配给自己,如果不是'' If it is '' , then 'None' will be assigned to it, as '' is considered falsey 如果是'' ,那么'None'将被分配给它,因为''被认为是假的

Try this: 尝试这个:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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