我的对象内部具有这样的功能:
$scope.createAuction = function () {
var auction = { auction: { langData: {} } };
if($scope.selected.tab === $scope.infoTabs[0]) {
auction.auction = {
type: 'car',
layout: $scope.selected.description
};
if(auction.auction.layout === 1) {
for(var i = 0, l = $scope.langInput.values.length; i < l; i++) {
/*
auction.auction.langData.push({
$scope.langInput.values.selected: {
name: $scope.inputs.auction_name_account[i + 1]
}
});
*/
console.log($scope.inputs.auction_name_account[i + 1]);
console.log($scope.langInput.values[i].selected);
}
}
}
console.log(JSON.stringify(auction));
}
$scope.langInput.values[i].selected
是pl
和eng
,但用户可以对其进行更改并添加或删除。
$scope.inputs.auction_name_account[i + 1]
是用户提供的上述语言的描述。另外,如果用户更改了以上语言,则必须以他选择的语言提供描述。
可以接受的数据很好,但是我不知道如何构建该对象,因此在函数完成工作后,它将看起来像这样:
{
"auction":{
"type":"account",
"layout":1
"langData":{
"pl":{
"name":"description in polish provided by user"
}
"eng":{
"name":"description in english provided by user"
}
}
}
}
我试图通过/* ... */
中的这段代码来构建它,但是它无法正常工作,给我带来语法错误。
好吧,我知道下面的这个JSON是上面的对象以外的其他对象,但是我以后再使用JSON.stringify()并没有什么关系,我只需要将对象绑定到langData对象就可以了。
修改
正如@Patrick Evans所建议的那样,我已经更改了代码,所以现在不再是代码中的代码了:
$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) {
//alert($scope.langInput.values.length);
for(var i = 0, l = $scope.langInput.values.length; i < l; i++) {
auction.auction.langData.push({
$scope.langInput.values.selected: {
name: $scope.inputs.auction_name_account[i + 1]
}
});
console.log($scope.inputs.auction_name_account[i + 1]);
console.log($scope.langInput.values[i].selected);
}
}
}
if($scope.selected.tab === $scope.infoTabs[1]) {}
if($scope.selected.tab === $scope.infoTabs[2]) {}
console.log(JSON.stringify(auction));
}
我仍然收到语法错误:
未捕获到的SyntaxError:意外令牌。
从此行
$scope.langInput.values.selected: {
也是我的IDE突出显示并说
:改为预期。给
答案 0 :(得分:2)
您像这样初始化auction
:
var auction = { auction: { langData: {} } };
但是您随后通过执行以下操作覆盖了auction
属性
auction.auction = {
type: 'car',
layout: $scope.selected.description
};
这意味着langData
上不再有auction.auction
属性。您可以重新创建该属性,或扩展auction
属性以保留所有先前存在的属性。
//recreate the property
auction.auction = {
type: 'car',
layout: $scope.selected.description,
langData:{}
};
//or extend the property
Object.assign(auction.auction,{
type:'car',
layout:$scope.selected.description,
});
//or extend by explicitly setting each property
auction.auction.type = 'car';
auction.auction.layout = $scope.selected.description,
要在langData
上实际放置数据,取决于您的意图。
如果要成为对象,则可以将该变量用作属性访问器,并以此方式设置
//if you actually meant to use $scope.langInput.values.selected
auction.auction.langData[$scope.langInput.values[i].selected] = {
name: $scope.inputs.auction_name_account[i + 1]
};
//or if values is an array
let propName = $scope.langInput.values[i];
auction.auction.langData[propName] = {
name: $scope.inputs.auction_name_account[i + 1]
};
否则,如果它应该是数组,则需要将其更改为数组,然后可以使用computed property names
创建对象auction.auction = {
type: 'car',
layout: $scope.selected.description,
langData:[] //<-- array instead of object
};
/*...*/
auction.auction.langData.push({
[$scope.langInput.values[i].selected]:{ //<--computed name syntax
name:$scope.inputs.auction_name_account[i + 1]
}
});