BT101
BT101

Reputation: 3836

Build object name and property by variables

I have function with object within like this:

$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 is pl and eng but user can change it and add new or delete one. $scope.inputs.auction_name_account[i + 1] is description in above languages provided by user. Also if user change above languages he has to provide description in languages he had chosen.

Consoled data is good but I don't know how to build this object so after function finish job it would look like this:

{  
   "auction":{  
      "type":"account",
      "layout":1
      "langData":{
          "pl":{
              "name":"description in polish provided by user"
          } 
          "eng":{
              "name":"description in english provided by user"
          }
      }
   }
}

I tried to build it by this code which is in /* ... */ but it doesn't work giving me syntax error.


well I know that this JSON below is other then object above but it doesn't really matter I'll use JSON.stringify() later on I only have problem to bind objects to langData object.


Edit

As @Patrick Evans suggest I've change my code so I'm not longer overwriting it in code now it is:

$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));
}

I'm still getting syntax error:

Uncaught SyntaxError: Unexpected token .

from this line

$scope.langInput.values.selected: { also my IDE is highlighting it and saying

: expected instead . given

Upvotes: 0

Views: 68

Answers (1)

Patrick Evans
Patrick Evans

Reputation: 42746

You initialized auction like so:

var auction = { auction: { langData: {} } };

But you then later overwrite the auction property by doing

auction.auction = {
  type: 'car',
  layout: $scope.selected.description
};

This means there is no longer a langData property on auction.auction. You can recreate that property, or extend the auction property keeping any previous properties that already existed.

//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,

For actually putting data on the langData, it depends on what you meant for it to be.

If it is meant to be an object you can use the variable as a property accessor and set it that way

//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]
};

Otherwise if it supposed to be an array, you need to change it to be an array, and then you can create your object using 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]
  }
});

Upvotes: 2

Related Questions