Возможность 1
function increment(...elem) {
return elem.map(([x, y]) => x + y);
}
let a = 1;
let b = 1;
let c = 1;
let d = 20;
let e = 12;
let f = 8;
[
a,
b,
c
] = increment([a, 1], [b, 2], [c, 3]);
console.log(`${a}, ${b}, ${c}`);
[
d,
e,
f
] = increment([d, 10], [e, 2], [f, 5]);
console.log(`${d}, ${e}, ${f}`);
Возможность 2
function increment(...elem) {
return elem.reduce((tmp, x, xi) => xi % 2 ? [
...tmp,
x + elem[xi - 1],
] : tmp, []);
}
let a = 1;
let b = 1;
let c = 1;
let d = 20;
let e = 12;
let f = 8;
[
a,
b,
c
] = increment(a, 1, b, 2, c, 3);
console.log(`${a}, ${b}, ${c}`);
[
d,
e,
f
] = increment(d, 10, e, 2, f, 5);
console.log(`${d}, ${e}, ${f}`);
Возможность 3
function increment(obj, keysAndVal) {
keysAndVal.forEach(([x, y]) => (obj[x] += y));
}
const vars = {
a: 1,
b: 1,
c: 1,
d: 20,
e: 12,
f: 8,
};
increment(vars, [
['a', 1],
['b', 5],
['c', 8],
]);
console.log(`${vars.a}, ${vars.b}, ${vars.c}`);
increment(vars, [
['d', 10],
['e', 2],
['f', 3],
]);
console.log(`${vars.d}, ${vars.e}, ${vars.f}`);
Возможность 4
function increment(elem, indexes, values) {
indexes.forEach((x, xi) => (elem[x] += values[xi]));
}
const vals = [
1,
1,
1,
20,
12,
8,
];
increment(vals, [0, 1, 2], [10, 20, 30]);
console.log(`${vals[0]}, ${vals[1]}, ${vals[2]}`);
increment(vals, [3, 4, 5], [5, 10, 15]);
console.log(`${vals[3]}, ${vals[4]}, ${vals[5]}`);