我有一个ingredient.model,它扩展了shoppingItem.model,以实现与购物模型中相同的参数,以便稍后将配料数组连接到我的购物清单数组。我尝试了以下代码,但没有将其添加到我的购物清单数组中。
addIngredientsToShoppingList(ingredients:Ingredient[]){
this.shoppingList.concat(ingredients);
console.log(this.shoppingList.slice());
this.shoppingListChanged.emit(this.shoppingList.slice());
}
这里的配料类型是从shoppingItem.model继承的,我是否必须首先将配料类型转换为shoppingItem类型才能将其连接到我的shoppingList中,或者我该怎么办?
发布于 2019-03-01 12:53:01
合并两个数组:
它采用所有数组进行连接,并映射a(整个数组)的一个赋值元素的结果,然后将c映射为项b和项bi。
var array1 = [{ id: "abdc4051", date: "2017-01-24" }, { id: "abdc4052", date: "2017-01-22" }],
array2 = [{ id: "abdc4051", name: "ab" }, { id: "abdc4052", name: "abc" }],
result = [array1, array2].reduce((a, b) => a.map((c, i) => Object.assign({}, c, b[i])));
console.log(result);
发布于 2019-03-01 13:41:39
对不起,我犯了个错误,我没有将数组列表分配给购物清单,因此购物清单没有更新
this.shoppingList = this.shoppingList.concat(ingredients);
谢谢你的帮助
https://stackoverflow.com/questions/54938022
复制相似问题