- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我对所有这一切都很陌生,所以请放轻松,我已经搜索了所有我能想到的东西,但我就是想不通。
我目前正在构建一个 ionic3 应用程序并设置了购物 list ,以便用户可以将食谱添加到购物 list - 这工作正常 - 但我想调整它以便如果添加多个相同的项目,而不是具有相同项目的两个实例,它更新第一个项目以显示它具有新数据。我可以通过服务发送项目并在另一个页面上检索它们,我可以按字母顺序对它们进行排序,但我无法使用合并功能。
非常感谢您的帮助。
这是(希望)相关代码:
import { Injectable } from "@angular/core";
import { Storage } from "@ionic/storage";
import { SLIngredient } from "../data/shopping-interface";
@Injectable()
export class ShoppingService {
storedDay: SLIngredient[] = []
storedWeek: SLIngredient[]
userSL: SLIngredient[] = []
sortedList: SLIngredient[] = []
mergedItem: SLIngredient;
mergedList: SLIngredient[] = [];
constructor(private storage: Storage) {}
getShoppingDay(day: number) {
return this.storage.get('shoppingDay/' + day)
.then(
(storedDay: SLIngredient[]) => {
this.storedDay = storedDay != null ? storedDay : [];
return this.storedDay.slice();
}
)
}
getShoppingWeek(id: number) {
return this.storage.get('shoppingWeek/' + id)
.then(
(storedWeek: SLIngredient[]) => {
this.storedWeek = storedWeek != null ? storedWeek : [];
return this.storedWeek.slice();
}
)
}
addItem(name: string, amount: number, measurement: string, completed: boolean) {
this.userSL.push(new SLIngredient(name, amount, measurement, completed));
}
addItems(items: SLIngredient[]) {
this.userSL.push(...items);
this.sort()
this.setUserSL();
console.log(this.userSL)
}
getItems() {
this.getUserSL;
return this.userSL.slice();
}
sort() {
this.sortedList = this.userSL.sort((itemA: SLIngredient, itemB: SLIngredient) => {
const a = itemA.name.toLowerCase()
const b = itemB.name.toLowerCase()
if (a > b) return 1;
if (a < b) return -1;
if (a == b) {
this.merge(itemA, itemB);
}
return 0
})
this.userSL = this.sortedList
}
merge(itemA: SLIngredient, itemB: SLIngredient) {
const newAmount = +itemA.amount + +itemB.amount
const itemC = new SLIngredient(itemA.name, newAmount, itemA.measurement, itemA.completed)
this.sortedList.splice(this.sortedList.indexOf(itemA), 1, itemC);
this.sortedList.splice(this.sortedList.indexOf(itemB), 1);
// this.sortedList = Object.assign(itemA, itemB, itemC)
// this.mergedList.push(new SLIngredient(merge.name, merge.amount, merge.measurement, merge.completed))
// this.sortedList.push(this.mergedItem)
}
setUserSL() {
this.storage.set('userSL', this.userSL)
}
getUserSL() {
return this.storage.get('userSL')
.then(
(userSL: SLIngredient[]) => {
this.userSL = userSL != null ? userSL : [];
return
this.userSL.slice();
}
)
}
}
.sort() 函数是该函数运行的地方,我发现问题出在 .merge() 函数上。
这是界面:
export class SLIngredient {
constructor(
public name: string,
public amount: number,
public measurement: string,
public completed: boolean
){}
我真的很感谢任何人可以提供的任何形式的帮助,因为我对这一切都很陌生。
再次感谢。
编辑
当我尝试打电话时
sort(){
this.userSL.reduce((reduced, element) => {
const index = reduced.findIndex(r => r.name.toLowerCase() === element.name.toLowerCase());
if (index === -1) return [...reduced, element];
reduced[index].amount += element.amount;
return reduced;
}, []).sort((a, b) => {
const nameA = a.name.toLowerCase()
const nameB = b.name.toLowerCase()
if (nameA > nameB) return 1;
if (nameA < nameB) return -1;
return 0
})
}
这是在将两个相同的食谱添加到购物 list 后我在日志中得到的输出:
(9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {name: "rolled oats", amount: "1", measurement: "cup"}
1: {name: "milled flax seed", amount: "1-2", measurement: "tbsp"}
2: {name: "ground cinnamon", amount: "1", measurement: "tsp"}
3: {name: "non-diary milk of your choice - Coconut is best", amount: "1.5", measurement: "cups"}
4: {name: "desiccated coconut", amount: "1", measurement: "tbsp"}
5: {name: "banana", amount: "1", prep: "sliced"}
6: {name: "frozen mixed berries or fresh seasonal berries (optional)", amount: "1", measurement: "handful"}
7: {name: "raisins (optional)", amount: "1", measurement: "handful"}
8: {name: "cocoa powder (optional)", amount: "1", measurement: "tsp"}
length: 9
__proto__: Array(0)
(18) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {name: "rolled oats", amount: "11", measurement: "cup"}
1: {name: "milled flax seed", amount: "1-21-2", measurement: "tbsp"}
2: {name: "ground cinnamon", amount: "11", measurement: "tsp"}
3: {name: "non-diary milk of your choice - Coconut is best", amount: "1.51.5", measurement: "cups"}
4: {name: "desiccated coconut", amount: "11", measurement: "tbsp"}
5: {name: "banana", amount: "11", prep: "sliced"}
6: {name: "frozen mixed berries or fresh seasonal berries (optional)", amount: "11", measurement: "handful"}
7: {name: "raisins (optional)", amount: "11", measurement: "handful"}
8: {name: "cocoa powder (optional)", amount: "11", measurement: "tsp"}
9: {name: "rolled oats", amount: "11", measurement: "cup"}
10: {name: "milled flax seed", amount: "1-21-2", measurement: "tbsp"}
11: {name: "ground cinnamon", amount: "11", measurement: "tsp"}
12: {name: "non-diary milk of your choice - Coconut is best", amount: "1.51.5", measurement: "cups"}
13: {name: "desiccated coconut", amount: "11", measurement: "tbsp"}
14: {name: "banana", amount: "11", prep: "sliced"}
15: {name: "frozen mixed berries or fresh seasonal berries (optional)", amount: "11", measurement: "handful"}
16: {name: "raisins (optional)", amount: "11", measurement: "handful"}
17: {name: "cocoa powder (optional)", amount: "11", measurement: "tsp"}
length: 18
__proto__: Array(0)
第二次编辑
这是我仍然有问题的部分:
addItems(items: SLIngredient[]){
this.userSL.push(...items);
this.sort()
this.setUserSL();
console.log(this.userSL)
}
sort(){
this.mergedList = this.userSL.reduce((reduced, element) => {
const index = reduced.findIndex(r => r.name.toLowerCase() === element.name.toLowerCase());
if (index === -1) return [...reduced, element];
reduced[index].amount += element.amount;
return reduced;
}, []).sort((a, b) => {
const nameA = a.name.toLowerCase()
const nameB = b.name.toLowerCase()
if (nameA > nameB) return 1;
if (nameA < nameB) return -1;
return 0
})
this.userSL = this.mergedList
}
setUserSL(){
this.storage.set('userSL', this.userSL)
}
所以我调用了 addItems() 函数,它将它添加到数组中,但它也会立即合并它,然后出于某种原因,它认为该成分本身有 2 个,所以当它再次添加时,它会加倍,而不仅仅是加一个。
另一个问题是它显示更新后的值,而不是仅仅将其推送到可以在其他地方显示的数组。
(9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {name: "banana/s", amount: 1, prep: "mashed"}
1: {name: "rolled oats", amount: 1, measurement: "cup"}
2: {name: "milled flaxseed", amount: 2, measurement: "tbsp"}
3: {name: "ground cinnamon", amount: 1, measurement: "tsp"}
4: {name: "non-dairy milk of your choice (I use Almond)", amount: 1.5, measurement: "cup/s"}
5: {name: "frozen mixed berries or fresh seasonal berries (optional)", amount: 1, measurement: "handful/s"}
6: {name: "raisins (optional)", amount: 1, measurement: "handful/s"}
7: {name: "cocoa powder (optional)", amount: 1, measurement: "tsp"}
8: {name: "desiccated coconut (optional)", amount: 2, measurement: "tsp"}
变成:
(9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {name: "banana/s", amount: 2, prep: "mashed"}
1: {name: "cocoa powder (optional)", amount: 2, measurement: "tsp"}
2: {name: "desiccated coconut (optional)", amount: 4, measurement: "tsp"}
3: {name: "frozen mixed berries or fresh seasonal berries (optional)", amount: 2, measurement: "handful/s"}
4: {name: "ground cinnamon", amount: 2, measurement: "tsp"}
5: {name: "milled flaxseed", amount: 4, measurement: "tbsp"}
6: {name: "non-dairy milk of your choice (I use Almond)", amount: 3, measurement: "cup/s"}
7: {name: "raisins (optional)", amount: 2, measurement: "handful/s"}
8: {name: "rolled oats", amount: 2, measurement: "cup"}
length: 9
__proto__: Array(0)
成为
(9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {name: "banana/s", amount: 4, prep: "mashed"}
1: {name: "cocoa powder (optional)", amount: 4, measurement: "tsp"}
2: {name: "desiccated coconut (optional)", amount: 8, measurement: "tsp"}
3: {name: "frozen mixed berries or fresh seasonal berries (optional)", amount: 4, measurement: "handful/s"}
4: {name: "ground cinnamon", amount: 4, measurement: "tsp"}
5: {name: "milled flaxseed", amount: 8, measurement: "tbsp"}
6: {name: "non-dairy milk of your choice (I use Almond)", amount: 6, measurement: "cup/s"}
7: {name: "raisins (optional)", amount: 4, measurement: "handful/s"}
8: {name: "rolled oats", amount: 4, measurement: "cup"}
length: 9
__proto__: Array(0)
每次我添加新内容时它都会翻倍。包含成分的实际页面(应该说大多数事情中的一种)如下所示:
再次感谢您的所有意见。
最佳答案
您要在排序函数中更改数组的结构,这是大忌。排序函数返回一个根据排序函数排序的新数组,它不会改变原始数组。您应该首先使用 reduce
合并重复项,然后对数组进行排序。
userSL.reduce((reduced, element) => {
const index = reduced.findIndex(r => r.name.toLowerCase() === element.name.toLowerCase();
if (index === -1) return [...reduced, element];
reduced[index].amount += element.amount;
return reduced;
}, []).sort((a, b) => {
const nameA = a.name.toLowerCase()
const nameB = b.name.toLowerCase()
if (nameA > nameB) return 1;
if (nameA < nameB) return -1;
return 0
})
在 reduce
中,我们将一个元素一个一个地添加到结果数组(减少),但是如果我们发现当前元素已经被添加,我们只需更新已添加元素的数量。排序是直接的,因为数组已经合并。
这不是最有效的实现,它可能无法编译(我直接写在这里),但想法很简单,应该可行。
关于Angular Ionic3 TypeScript - 从数组拼接后未定义的 "TypeError: Cannot read property ' 名称',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53672832/
我已经写了并且 npm 发布了这个:https://github.com/justin-calleja/pkg-dependents 现在我正在用 Typescript 编写这个包:https://g
我有一个函数,我想在 TypeScript 中模拟它以进行测试。在我的测试中,我只关心 json和 status .但是,当使用 Jest 的 jest.spyOn 时我的模拟函数的类型设置为返回 h
我正在使用一个库 (Axios),它的包中包含 Typescript 声明。 我想声明一个将 AxiosResponse(在库的 .d.ts 文件中声明)作为参数的函数。我有以下内容: functio
我是 Typescript 的新手。我想使用 将一个 Typescript 文件加载到另一个 Typescript 文件中标签。 我做了一些事情,但它不起作用!请帮助我。 first.ts: imp
为什么我会收到下面屏幕截图中显示的错误? Atom 说我的 tsconfig.json“项目文件包含无效选项”用于 allowJs、buildOnSave 和 compileOnSave。 但是应该允
所以我正在创建一个 TypeScript 库,我可以轻松地将所有生成的 JS 文件编译成一个文件。有没有办法将所有 .ts 和 .d.ts 编译成一个 .ts 文件? 除了支持 JS 的版本(较少的智
Microsoft Research 提供了一种名为Safer TypeScript 的新 TypeScript 编译器变体: http://research.microsoft.com/en-us/
我需要这个来在单个文件中分发 TypeScript 中的库。有没有办法将多个 typescript 文件合并到(一个js文件+一个 typescript 定义)文件中? 最佳答案 要创建一个库,您可以
用例:我想知道一个函数在 typescript 中执行需要多少时间。我想为此目的使用装饰器。我希望装饰器应该返回时间以便(我可以进一步使用它),而不仅仅是打印它。 例如: export functio
我想检查一个类型是否可以为 null,以及它是否具有值的条件类型。 我尝试实现 type IsNullable = T extends null ? true : false; 但是好像不行 type
我的问题是基于这个 question and answer 假设我们有下一个代码: const myFn = (p: { a: (n: number) => T, b: (o: T) => v
我知道双重否定前缀,我知道 TypeScript 的单后缀(非空断言)。 但是这个双后缀感叹号是什么? /.*验证码为(\d{6}).*/.exec(email.body!!)!![1] 取自here
我正在使用以下文件结构在 Webstorm 中开发一个项目 | src | ... | many files | types | SomeInterface |
在 TypeScript 类中,可以为属性声明类型,例如: class className { property: string; }; 如何在对象字面量中声明属性的类型? 我试过下面的代码,但它
我正在寻找一种在不丢失推断类型信息的情况下将 TypeScript 中的文字值限制为特定类型的好方法。 让我们考虑一个类型Named,它保证有一个名字。 type Named = { name:
在 TypeScript 中,我想创建一个联合类型来表示属于一个或多个不同类型的值,类似于 oneOf在 OpenAPI或 JSON Schema .根据a previous answer on a
type Func = (foo:string) => void // function expression const myFunctionExpression:Func = function(f
假设我有一个联合类型,我正在使用类似 reducer 的 API 调用模式,看起来像这样: type Action = { request: { action: "create
我在 typescript 中有以下去抖功能: export function debounce( callback: (...args: any[]) => void, wait: numb
在 Vue3 的 defineComponent 函数中,第一个泛型参数是 Props,所以我在这里使用 Typescript 接口(interface)提供我的 props 类型。喜欢: expor
我是一名优秀的程序员,十分优秀!