javascript - Javascript 对象值变量
问题描述
我想在 js 对象中进行一些计算。这可能吗?
foo: [
{
value: 1000
target:50
process: (target*value)/100
},
{
value: 500
target:100
process: (target*value)/100
}]
process
密钥应从和value
计算target
。有没有办法做到这一点?js
解决方案
你可以做process
一个吸气剂:
const foo = [
{
value: 1000,
target: 50,
get process() {
return (this.target * this.value) / 100;
}
},
{
value: 500,
target: 100,
get process() {
return (this.target * this.value) / 100;
}
}
];
然后使用属性访问:
console.log(foo[0].process); //=> 500
console.log(foo[1].process); //=> 500
推荐阅读
- three.js - Fabric JS 2D线条转换成3Dviews
- javascript - 如何在使用 JavaScript 播放歌曲时加载下一首音频歌曲?
- javascript - python烧瓶停用复选框并获取ID
- visual-studio-code - VS Code Emmet 有一种方法可以仅在选项卡上触发扩展并在 Enter 上禁用
- vba - 将数据从列表框插入表格
- c - 如何更新结构中的现有元素?
- jdbc - 配置 Informix 数据库,使其可以通过 JDBC 访问
- java - 如何通过我的公式从 MySQL 数据库中获取和排序行?
- java - 如何仅使用 doPost() 运行 Web 应用程序?
- javascript - 使用 create-react-app 时 npm start 未运行