我正在使用以下代码
$scope.createAuction = function () {
var auction = { auction: { langData: {} } };
if($scope.selected.tab === $scope.infoTabs[0]) {
Object.assign(auction.auction, {
type: 'account',
layout: $scope.selected.description
});
if(auction.auction.layout === 1) {
for(var i = 0, l = $scope.langInput.values.length; i < l; i++) {
auction.auction.langData[$scope.langInput.values[i].selected] = {
name: $scope.inputs.auction_name_account[i + 1]
};
}
for(var i = 0, l = $scope.langInput.values.length; i < l; i++) {
auction.auction.langData[$scope.langInput.values[i].selected] = {
description: 'a'
}
}
}
}
console.log(JSON.stringify(auction));
}
,以便将值添加到用户提供的对象中。我正在使用循环,因为用户可以更改输入的数量。
我想要实现的是我的对象看起来像这样:
"auction": {
"type": "car",
"layout": 1,
"langData": {
"pl": {
"name": "nazwa aukcji w pl",
"description": "opis aukcji w pl",
},
"eng": {
"name": "name auction eng",
"description": "description auction eng",
},
但是我的代码正在覆盖"pl"
和"eng"
,所以结果是我只能看到描述。
答案 0 :(得分:1)
在第二个循环中,插入description
属性,而不是覆盖对象:
for(var i = 0, l = $scope.langInput.values.length; i < l; i++) {
auction.auction.langData[$scope.langInput.values[i].selected].description = 'a';
}
如果您愿意,还可以组合循环:
for (var i = 0, l = $scope.langInput.values.length; i < l; i++) {
auction.auction.langData[$scope.langInput.values[i].selected] = {
name: $scope.inputs.auction_name_account[i + 1],
description: 'a'
};
}