[英]How to increment value of object property using javascript?
i have a object named output like below我有一个名为 output 的 object,如下所示
const output = {
count {
books: 3,
pens: 2,
name: 'first',
}
}
i want to increment the books value by 1, name value to 'second' and leave pens value to be same.我想将书籍值增加 1,将名称值增加到“第二”,并使笔值保持不变。
what i have tried?我试过什么?
const books = output.count.books + 1;
const pens = output.count.pens;
const name = 'second';
output = {
books: books,
pens: pens,
name: name,
}
How can i rewrite this code?我该如何重写这段代码? could soemone help me with this.
soemone 可以帮我解决这个问题。 thanks.
谢谢。
You missed ' : ' in your object after 'count' and also you tried to override 'output' which is a constant.您在“计数”之后错过了 object 中的“ : ”,并且您还尝试覆盖常量“输出”。
const output = { count: {books:3,pens:2,name:'first'} } const books = output.count.books + 1; const pens = output.count.pens; const name = 'second'; let result = { books: books, pens: pens, name: name, } console.log(result);
I would just increment the values of books and update the name:我只会增加书籍的价值并更新名称:
const output = { count: {books:3,pens:2,name:'first'} } output.count.books++; output.count.name = 'second'; console.log(output);
try this way试试这个方法
let output = { count: { books: 3, pens: 2, name: 'first', } } const books = output.count.books + 1; const pens = output.count.pens; let name = output.count.name; name = "second"; output = { 'books': books, 'pens': pens, 'name': name, } console.log(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.