Home > javascript > How to push key value pair inside an object key dynamically in javascript

How to push key value pair inside an object key dynamically in javascript

January 28Hits:2
Advertisement

I have defined an object and trying to push key value pairs in the object on a key that is dynamically defined.

var investmentByParts = {};     $scope.investedCommodities = [];     for (var z = 0; z < $scope.userStockList.length; z++) {                        var tempArray = {               Name: $scope.userStockList[z].Name,               Symbol: $scope.userStockList[z].Symbol,               Weight: parseFloat(keyValue.Weight),             }             investmentByParts[$scope.userStockList[z].Name].push(tempArray);      }      makeInvestmentDistributionChart(); 

But whenever I am trying to push tempArray in investmentByParts object, I get the error Cannot read property 'push' of undefined. Can't I dynamically define the key and add key value pairs into it?

Tags:javascript

Related Articles

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.911 s.