gpt4 book ai didi

javascript - 如何在 Angular js中的现有数组中添加新键?

转载 作者:行者123 更新时间:2023-12-02 16:06:53 27 4
gpt4 key购买 nike

我定义了空白数组 $scope.order,在表单上提交数据获取并创建新数组,然后与现有数组合并。

.controller('GuestDetailsCtrl',function($scope){
$scope.order = [];
$scope.itemDetails = function() {
// Here data get after form submiti have array arrieve from form submit.
$scope.order=$scope.order.push({name:$scope.item.name,price:$scope.item.price});
}
});

我想要这样的结果。

$scope.order = [{name:'abc',price:100},{name:'pqr',price:80},{name:'xyz',price:50}];

When itemDetails() call at that time array merge with new data.

最佳答案

push 对数组进行就地操作。简单

$scope.order = [];
$scope.itemDetails = function() {
// Here data get after form submiti have array arrieve from form submit.
$scope.order.push({name:$scope.item.name,price:$scope.item.price});
}

(不分配它),这应该可以!

关于javascript - 如何在 Angular js中的现有数组中添加新键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635512/

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