Object.assign 对象数组
Object.assign for an array of objects
所以我可以在这样的对象上使用 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 = ingredients.flat().map(p => Object.assign(p));
也根据阅读尝试了一些不同的迭代this
[更正x2]
也许是这样的:
copy = ingredients.map(o=>Object.assign({},o))
所以我可以在这样的对象上使用 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 = ingredients.flat().map(p => Object.assign(p));
也根据阅读尝试了一些不同的迭代this
[更正x2] 也许是这样的:
copy = ingredients.map(o=>Object.assign({},o))