1

I am new to angular key,value concepts. Down below is my json object-> key,value.

key->String value->object

{
   "Value 3": {
    "paymentRequestMethod": null,
    "bankHolderName": "BANKHOLDER3",
    "routingNumber": "278723",
    "bankAccountNumber": "BANKACCOUNT1",
    "reEnteredBankAccountNumber": null,
    "accountType": "savings",
    "bankName": "BANK3",
    "accountNumberVerificationValue": null,
    "encryptedBankAccountNumber": null,
    "enrollEftIndicator": false,
    "status": null
  },
  "Value 4": {
    "paymentRequestMethod": null,
    "bankHolderName": "BANKHOLDER4",
    "routingNumber": "278724",
    "bankAccountNumber": "BANKACCOUNT1",
    "reEnteredBankAccountNumber": null,
    "accountType": "savings",
    "bankName": "BANK4",
    "accountNumberVerificationValue": null,
    "encryptedBankAccountNumber": null,
    "enrollEftIndicator": false,
    "status": null
  },
  "Value 1": {
    "paymentRequestMethod": null,
    "bankHolderName": "BANKHOLDER1",
    "routingNumber": "278721",
    "bankAccountNumber": "BANKACCOUNT1",
    "reEnteredBankAccountNumber": null,
    "accountType": "savings",
    "bankName": "BANK1",
    "accountNumberVerificationValue": null,
    "encryptedBankAccountNumber": null,
    "enrollEftIndicator": false,
    "status": null
  },
  "Value 2": {
    "paymentRequestMethod": null,
    "bankHolderName": "BANKHOLDER2",
    "routingNumber": "278722",
    "bankAccountNumber": "BANKACCOUNT1",
    "reEnteredBankAccountNumber": null,
    "accountType": "savings",
    "bankName": "BANK2",
    "accountNumberVerificationValue": null,
    "encryptedBankAccountNumber": null,
    "enrollEftIndicator": false,
    "status": null
  },
  "Value 0": {
    "paymentRequestMethod": null,
    "bankHolderName": "BANKHOLDER0",
    "routingNumber": "278720",
    "bankAccountNumber": "BANKACCOUNT1",
    "reEnteredBankAccountNumber": null,
    "accountType": "savings",
    "bankName": "BANK0",
    "accountNumberVerificationValue": null,
    "encryptedBankAccountNumber": null,
    "enrollEftIndicator": false,
    "status": null
  }
}

i seen lot of examples which has labels for both key and Value, so we can able to iterate using angular.foreach(). My question is how to get value by passing key without using labels(like above json).is it possible??

Thanks in Advance.

Nitheesh
  • 19,238
  • 3
  • 22
  • 49

2 Answers2

2

Try this one.

<!DOCTYPE html>
<html ng-app="myApp">

<head>
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script>
        var myApp = angular.module("myApp", []);
        myApp.controller("AppControler", function ($scope) {

            $scope.Options= ["Value 0", "Value 1", "Value 2", "Value 3", "Value 4"];
            $scope.DataItems = {
                              "Value 3": {
                                "paymentRequestMethod": null,
                                "bankHolderName": "BANKHOLDER3",
                                "routingNumber": "278723",
                                "bankAccountNumber": "BANKACCOUNT1",
                                "reEnteredBankAccountNumber": null,
                                "accountType": "savings",
                                "bankName": "BANK3",
                                "accountNumberVerificationValue": null,
                                "encryptedBankAccountNumber": null,
                                "enrollEftIndicator": false,
                                "status": null
                              },
                              "Value 4": {
                                "paymentRequestMethod": null,
                                "bankHolderName": "BANKHOLDER4",
                                "routingNumber": "278724",
                                "bankAccountNumber": "BANKACCOUNT1",
                                "reEnteredBankAccountNumber": null,
                                "accountType": "savings",
                                "bankName": "BANK4",
                                "accountNumberVerificationValue": null,
                                "encryptedBankAccountNumber": null,
                                "enrollEftIndicator": false,
                                "status": null
                              },
                              "Value 1": {
                                "paymentRequestMethod": null,
                                "bankHolderName": "BANKHOLDER1",
                                "routingNumber": "278721",
                                "bankAccountNumber": "BANKACCOUNT1",
                                "reEnteredBankAccountNumber": null,
                                "accountType": "savings",
                                "bankName": "BANK1",
                                "accountNumberVerificationValue": null,
                                "encryptedBankAccountNumber": null,
                                "enrollEftIndicator": false,
                                "status": null
                              },
                              "Value 2": {
                                "paymentRequestMethod": null,
                                "bankHolderName": "BANKHOLDER2",
                                "routingNumber": "278722",
                                "bankAccountNumber": "BANKACCOUNT1",
                                "reEnteredBankAccountNumber": null,
                                "accountType": "savings",
                                "bankName": "BANK2",
                                "accountNumberVerificationValue": null,
                                "encryptedBankAccountNumber": null,
                                "enrollEftIndicator": false,
                                "status": null
                              },
                              "Value 0": {
                                "paymentRequestMethod": null,
                                "bankHolderName": "BANKHOLDER0",
                                "routingNumber": "278720",
                                "bankAccountNumber": "BANKACCOUNT1",
                                "reEnteredBankAccountNumber": null,
                                "accountType": "savings",
                                "bankName": "BANK0",
                                "accountNumberVerificationValue": null,
                                "encryptedBankAccountNumber": null,
                                "enrollEftIndicator": false,
                                "status": null
                              }
                };
            
            $scope.OnOptionSelect = function(){
                console.log($scope.DataItems[$scope.SelectedOption]);
            }
        });
    </script>
</head>

<body ng-controller="AppControler">
    <select ng-options="option for option in Options" ng-change="OnOptionSelect()" ng-model="SelectedOption"></select>
</body>

</html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
1

You can do it using javascript also.

Try this

<script>

  var tempObj = {
    "Value 3": {
     "paymentRequestMethod": null,
     "bankHolderName": "BANKHOLDER3",
     "routingNumber": "278723",
     "bankAccountNumber": "BANKACCOUNT1",
     "reEnteredBankAccountNumber": null,
     "accountType": "savings",
     "bankName": "BANK3",
     "accountNumberVerificationValue": null,
     "encryptedBankAccountNumber": null,
     "enrollEftIndicator": false,
     "status": null
    },
    "Value 4": {
     "paymentRequestMethod": null,
     "bankHolderName": "BANKHOLDER4",
     "routingNumber": "278724",
     "bankAccountNumber": "BANKACCOUNT1",
     "reEnteredBankAccountNumber": null,
     "accountType": "savings",
     "bankName": "BANK4",
     "accountNumberVerificationValue": null,
     "encryptedBankAccountNumber": null,
     "enrollEftIndicator": false,
     "status": null
    },
    "Value 1": {
     "paymentRequestMethod": null,
     "bankHolderName": "BANKHOLDER1",
     "routingNumber": "278721",
     "bankAccountNumber": "BANKACCOUNT1",
     "reEnteredBankAccountNumber": null,
     "accountType": "savings",
     "bankName": "BANK1",
     "accountNumberVerificationValue": null,
     "encryptedBankAccountNumber": null,
     "enrollEftIndicator": false,
     "status": null
    },
    "Value 2": {
     "paymentRequestMethod": null,
     "bankHolderName": "BANKHOLDER2",
     "routingNumber": "278722",
     "bankAccountNumber": "BANKACCOUNT1",
     "reEnteredBankAccountNumber": null,
     "accountType": "savings",
     "bankName": "BANK2",
     "accountNumberVerificationValue": null,
     "encryptedBankAccountNumber": null,
     "enrollEftIndicator": false,
     "status": null
    },
    "Value 0": {
     "paymentRequestMethod": null,
     "bankHolderName": "BANKHOLDER0",
     "routingNumber": "278720",
     "bankAccountNumber": "BANKACCOUNT1",
     "reEnteredBankAccountNumber": null,
     "accountType": "savings",
     "bankName": "BANK0",
     "accountNumberVerificationValue": null,
     "encryptedBankAccountNumber": null,
     "enrollEftIndicator": false,
     "status": null
    }
   }

   console.log(tempObj['Value 0']);
   console.log(tempObj['Value 1']);
   console.log(tempObj['Value 4']);
</script>
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51