admin管理员组文章数量:1026983
var obj = {
bob : 14
jan : 2
}
If i add to this object, such as obj.bob = "12", is there a way to add this to make bob : 26 instead of replacing it with bob: 12? Thank you.
var obj = {
bob : 14
jan : 2
}
If i add to this object, such as obj.bob = "12", is there a way to add this to make bob : 26 instead of replacing it with bob: 12? Thank you.
Share Improve this question asked Feb 15, 2017 at 12:14 SelectfulSelectful 1073 silver badges15 bronze badges 2-
2
Increment current value ..
obj.bob += 12
– charlietfl Commented Feb 15, 2017 at 12:16 -
1
obj.bob = obj.bob ? obj.bob + 12 : + 12
– naortor Commented Feb 15, 2017 at 12:18
4 Answers
Reset to default 5You could use an ES6 feature, Proxy
The
Proxy
object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).
var obj = { bob: 14, jan: 2 },
p = new Proxy(obj, {
set: function(target, prop, value) {
target[prop] = (target[prop] || 0) + value;
}
});
p.bob = 10;
console.log(p.bob); // 24
p.jane = 42;
console.log(p.jane); // 42
So, you use this way.
var obj = {
bob : 14,
jan : 2
}
obj.bob += 12;
console.log(obj);
var obj = {
bob : 14,
jan : 2
}
obj.bob += 12;
console.log(obj.bob)
The simple, and almost certainly correct, answer is to use the addition assignment +=
operator, which adds the right operand to the left operand.
obj.bob += 12; //obj.bob is now 26
You may want to have a system where setting a property with the assignment operator =
actually adds the value to the property's existing value. This seems confusing and unlikely to be helpful, but it is technically possible using Object.defineProperty
:
let _bob = 14;
Object.defineProperty(obj, 'bob', {
set(newValue) {
_bob += Number(newValue);
},
get() {
return _bob;
}
});
obj.bob = 12;
console.log(obj.bob); // logs '26'
I can't believe this is ever likely to be desired behaviour, however.
var obj = {
bob : 14
jan : 2
}
If i add to this object, such as obj.bob = "12", is there a way to add this to make bob : 26 instead of replacing it with bob: 12? Thank you.
var obj = {
bob : 14
jan : 2
}
If i add to this object, such as obj.bob = "12", is there a way to add this to make bob : 26 instead of replacing it with bob: 12? Thank you.
Share Improve this question asked Feb 15, 2017 at 12:14 SelectfulSelectful 1073 silver badges15 bronze badges 2-
2
Increment current value ..
obj.bob += 12
– charlietfl Commented Feb 15, 2017 at 12:16 -
1
obj.bob = obj.bob ? obj.bob + 12 : + 12
– naortor Commented Feb 15, 2017 at 12:18
4 Answers
Reset to default 5You could use an ES6 feature, Proxy
The
Proxy
object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).
var obj = { bob: 14, jan: 2 },
p = new Proxy(obj, {
set: function(target, prop, value) {
target[prop] = (target[prop] || 0) + value;
}
});
p.bob = 10;
console.log(p.bob); // 24
p.jane = 42;
console.log(p.jane); // 42
So, you use this way.
var obj = {
bob : 14,
jan : 2
}
obj.bob += 12;
console.log(obj);
var obj = {
bob : 14,
jan : 2
}
obj.bob += 12;
console.log(obj.bob)
The simple, and almost certainly correct, answer is to use the addition assignment +=
operator, which adds the right operand to the left operand.
obj.bob += 12; //obj.bob is now 26
You may want to have a system where setting a property with the assignment operator =
actually adds the value to the property's existing value. This seems confusing and unlikely to be helpful, but it is technically possible using Object.defineProperty
:
let _bob = 14;
Object.defineProperty(obj, 'bob', {
set(newValue) {
_bob += Number(newValue);
},
get() {
return _bob;
}
});
obj.bob = 12;
console.log(obj.bob); // logs '26'
I can't believe this is ever likely to be desired behaviour, however.
本文标签: javascriptAdd Value if Same Key from ObjectStack Overflow
版权声明:本文标题:javascript - Add Value if Same Key from Object - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745642168a2160814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论