gpt4 book ai didi

javascript - 如何在 Angularjs 中创建嵌套的 json 对象

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:11 27 4
gpt4 key购买 nike

我想在 angularjs 中创建嵌套的 json 对象。我的目标是:

   {
"marketerId": 1,
"baskets": [
{
"customer": {
"phone": ""
},
"region": 1,
"orders": [
{
"bookId": 1,
"count": 5
},
{
"bookId": 2,
"count": 52
}
]
},
{
"customer": {
"phone": ""
},
"region": 1,
"orders": [
{
"bookId": 1,
"count": 12
},
{
"bookId": 2,
"count": 2
}
]
}
]
}

为了动态创建此对象,我编写了这段代码。假设订单和项目已经初始化,则创建表单。比如商品和订单的大小 2.有没有更好的方法来构建嵌套的json对象?

     <input ng-model="formData.marketerId" />
<div class="row" ng-repeat="item in items track by $index">
<input ng-model="formData.baskets[$index].customer.phone" />
<input ng-model="formData.baskets[$index].region" />
<div ng-repeat="order in orders track by $index">
<input type="text" ng-model=
"formData.baskets[$parent.$index].orders[$index].bookId">
<input type="text" ng-model=
"formData.baskets[$parent.$index].orders[$index].count">

</div>
</div>

最佳答案

你可以这样做:

$scope.data1 = [];
var firstObj = new Object();
firstObj.first = "value1";
firstObj.second = "value2";
$scope.encountersData.push(firstObj);

$scope.data2 = [];
var secondObj= new Object();
secondObj.third = "value3";
secondObj.fourth = "value4";
$scope.data2.push(secondObj);

关于javascript - 如何在 Angularjs 中创建嵌套的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847237/

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