javascript - 函数仅从数组中删除项目但不更改值
问题描述
我创建了一个类,它获取计算机部件及其成本,并根据选择的部件计算它们。目前我有两个功能,一个是在报价中添加更多部分,另一个是删除部分。它在删除或添加项目的意义上可以正常工作,但不会改变总成本。当我移除零件时,价格保持不变,如果我添加更多零件也是如此。我能做些什么来让它按预期工作。这是代码:
class PriceCalc {
Motherboard = 520.99;
RAM = 250.4;
SSD = 500.8;
HDD = 400.66;
Case = 375.5;
Monitor = 600.75;
Keyboard = 100.99;
Mouse = 25.5;
constructor(Obj) {
this.parts = Obj;
this.cost = "$" + Obj.reduce((a, b) => a + this[b], 0).toFixed(2);
this.retail ="$" +(Obj.reduce((a, b) => a + this[b], 0) +Obj.reduce((a, b) => a + this[b], 0) * 1.75).toFixed(2);
this.quote = "Your quote is " + this.retail;
}
add(item) {
this.parts = [...this.parts, item];
}
remove(item) {
this.parts = this.parts.filter((x) => x !== item);
}
}
quote4 = new PriceCalc(["RAM", "SSD", "Case", "Mouse"]);
console.log(quote4.parts);//Returns ["RAM", "SSD", "Case", "Mouse"]
console.log(quote4.cost);//Returns $1152.20
console.log(quote4.retail);//Returns $3168.55
console.log(quote4.quote);//Returns "Your quote is $3168.55"
quote4.remove("Case")
console.log(quote4.parts);//Returns ["RAM", "SSD", "Mouse"]
console.log(quote4.cost);//Returns $1152.20
console.log(quote4.retail);//Returns $3168.55
console.log(quote4.quote);//Returns "Your quote is $3168.55"
目前 this.cost/retail/quote 在添加或删除内容时不会更改,而如果添加或删除项目,则应修改它们。目前我可以更改值的唯一方法是手动更改被调用数组中的部分。我该如何解决这个问题?
解决方案
每当添加新项目或删除项目时,您都需要重新计算它们。由于它们需要从不同的地方(constructor
,add
和remove
)重新计算,因此一种新方法(例如称为calculate
或update
)非常适合,因为可重用代码应该分组在一个函数/方法中,这样您就不会重复自己。
此外,cost
andretail
应该是数字而不是字符串。您正在使用reduce
计算cost
,然后使用相同的reduce
两次来计算retail
应避免的情况。只需计算cost
并使用cost
来计算retail
。
最后,add
不remove
应该在每次删除或添加项目时创建新数组。
class PriceCalc {
Motherboard = 520.99;
RAM = 250.4;
SSD = 500.8;
HDD = 400.66;
Case = 375.5;
Monitor = 600.75;
Keyboard = 100.99;
Mouse = 25.5;
constructor(initialParts) { // always choose good names for your variables. initialParts makes more sense than Obj
this.parts = initialParts; // store the initial parts
calculate(); // calculate cost, retail and quote
}
add(item) {
this.parts.push(item); // use push instead of creating a new array (which is an overkill)
calculate(); // recalculate cost, retail and quote
}
remove(item) {
this.parts = this.parts.filter((x) => x !== item); // I'd rather use this https://stackoverflow.com/a/5767357 over filter but filter is shorter so OK
calculate(); // recalculate cost, retail and quote
}
calculate() { // the method that calculates cost, retail and quote
this.cost = this.parts.reduce((a, b) => a + this[b], 0); // calculate cost and store as a number instead of a string
this.retail = this.cost + this.cost * 1.75; // calculate retail using the value of this.cost and also store as a number (you can use this.retail = 2.75 * this.cost; which is the same)
this.quote = "Your quote is $" + this.retail.toFixed(2); // generate the quote (notice the added $ sign and the call to toFixed(2))
}
}
推荐阅读
- python - 将 `tf.Summary` 对象序列化为 `string` 张量
- csv - 将远程文件加载到 Power BI
- javascript - 为什么http服务器在6次请求后没有响应
- sql - 我想要一些 SQL 语法来检查值是否与其下面的不同
- regex - 我的 Apache Require expr RegEx 不起作用
- mysql - mysql 在更改大表时挂起
- c# - LINQ Query 选择数据并转换为特定的 json 格式
- javascript - 如何使用 nativeElement.focus() 与 ngFor 中的每个唯一输入以角度
- arrays - 从 json 数据填充的下拉列表
- java - Java - Maven 测试/单元测试在运行整个包时通过,但在单独运行测试时失败