gpt4 book ai didi

javascript - Object.assign 用于对象数组

转载 作者:行者123 更新时间:2023-12-02 21:00:02 24 4
gpt4 key购买 nike

所以我可以在这样的对象上使用 object.assign

let recipe = {"recipeId":1,"title":"Instant Pot® Chicken and Wild Rice Soup","directions":"Lorem ipsum dolor sit","imageLink":"https://images-gmi-pmc.edge-generalmills.com/60c3ebda-50a7-415e-8c66-14f6c9b93034.jpg","id":"1"}
this.editableRecipe = Object.assign({},recipe);

但是我在使用这样的对象列表来执行此操作时遇到困难:

let ingredients = [
{"ingredientId":1,"amount":1,"unit":"package","ingredient":"(20 oz) boneless skinless chicken thighs","note":"patted dry","id":"1"},
{"ingredientId":2,"amount":1,"unit":"teaspoon","ingredient":"salt22323","note":"","id":"2"},
{"ingredientId":3,"amount":0.5,"unit":"teaspoon","ingredient":"pepper","note":"","id":"3"},
{"ingredientId":4,"amount":2,"unit":"tablespoons","ingredient":"butter","note":"","id":"4"},
{"ingredientId":5,"amount":1,"unit":"package","ingredient":"(20 oz) boneless skinless chicken thighs","note":"patted dry","id":"5"},
{"ingredientId":6,"amount":1,"unit":"teaspoon","ingredient":"salt","note":"","id":"6"}
];

// this.editableIngredients = something for object assign for the array here

从我读到的内容来看,array.map 方法可能是最好的方法,但我在正确使用它时遇到了一些问题。

基于this我尝试了这样的事情: this.editableIngredients = Inventions.flat().map(p => Object.assign(p));

根据阅读 this 还尝试了一些不同的迭代

最佳答案

[更正x2]也许是这样的:

copy = ingredients.map(o=>Object.assign({},o))

关于javascript - Object.assign 用于对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61378792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com