gpt4 book ai didi

javascript - 我尝试使用push()在数组中添加值数组,但我不知道如何添加它

转载 作者:行者123 更新时间:2023-11-27 22:56:37 25 4
gpt4 key购买 nike

这个我的代码,我尝试对company1员工进行评估,但它创建了一个新的数组列表。有人可以帮助我吗?

添加员工姓名和 ID 后,数组将如下所示

[{companyName: "company1",companyAddress: "company address",staff: [{name:"men",id: "123"},{name:"boy",id: "1343"},{name:"john",id: "145"}]}]

<div ng-controller="MyCtrl">
<input type="text" ng-model="nameStaff" />
<input type="text" ng-model="idStaff" />
<button ng-click=addDetail()>add</button>
{{form}}
</div>

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
$scope.form = [{ companyName: "company1", companyAddress: "company address", staff: [{ name: "men", id: "123" }] }];
$scope.addDetail = function() {
$scope.form.push({ staff: [{ name: $scope.nameStaff, id: $scope.idStaff }] });
}
}

最佳答案

您正在推送到 form,但您想要推送到 form 中第一个对象的 staff:

$scope.form[0].staff.push({
name: $scope.nameStaff,
id: $scope.idStaff
});

关于javascript - 我尝试使用push()在数组中添加值数组,但我不知道如何添加它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545772/

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