Home > Enterprise >  how to increase the value of an object dependent on the key name [closed]
how to increase the value of an object dependent on the key name [closed]

Time:09-27

I want to increase the value depending on the key name. I have an array with scores:

[50,50,60,70,80,90,90,100]
Grade A: score<100 and score>=90
Grade B: score<90 and score>=80
Grade C: score<80 and score>=60
Grade D: score<60 and score>=0

In the end, I want to get an object with countable scores:

{A:0, B:0, C:0, D:0 };

CodePudding user response:

You could take a Proxy and change dependent properties.

const
    table = { S: 0, A: 0, B: 0, C: 0, D: 0, X: 0 },
    change = new Proxy(table, {
        set: function(obj, prop) {
            if (prop === 'B') obj.S  ;
            obj[prop]  ;
            return true;
        },
    });

change.B  ;

console.log(table);

  • Related