1

I am stuck on a issue where i need to access a nested JSON with dynamic variables. This is my JSON:

{
    "Account2": [{
        "accountId": "17467****",
        "containerId": "7454***",
        "path": "accounts\/17467*****\/containers\/7454***",
    }, {
        "accountId": "17467****",
        "containerId": "7519***",
        "path": "accounts\/17467****\/containers\/7519***",
    }],
    "Account 1": [{
        "accountId": "17661****",
        "containerId": "7483***",
        "path": "accounts\/17661****\/containers\/7483***",
    }]
}

Using AngluarJS on front-end i use this to print a table, here i can use the "path" variable to use a href and then make second API call based on url parameters like this:

<td><a href="/gui/tags/{{v1.path}}">View Container</a></td>

v1.path = accounts/17467*****/containers/7454***

Now to my problem, i want to send this call before clicking on table to show some data. The problem is that when accessing the JSON that has dynamic variable as you can see: "Account2", "Account1". I cannot use: `$scope.GTMcontainersAccount = response.data.ACCOUNT1;

because "account1" is dynamic and changes from user to user.

Someone has an idea? regEx?

UPDATED:

when i use:

 $scope.GTMcontainersAccount = response.data;
                    $scope.keys = Object.keys(response.data);
                    for(n in $scope.keys)
                    {
                       $scope.asim = $scope.keys[n];
                       console.log($scope.asim);
                       console.log(response.data[$scope.asim]);
                    }

This gives me this result:

enter image description here

So i have to write like:

console.log(response.data[$scope.asim][0].accountId);

But this gives me only row of "account2" not "account1"

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Asim
  • 936
  • 2
  • 10
  • 29
  • 1
    Use Object.keys() ? Iterate over all the keys? – Adrian Nov 14 '17 at 12:45
  • @Adriani6 console.log(Object.keys(response.data)); gives me "account1" and "account2" not the key:value itself – Asim Nov 14 '17 at 12:53
  • 2
    @Asim Question for 1 million: if you have object and a key, how to access value for this key? – dfsq Nov 14 '17 at 12:59
  • i usally can access value for a key by using: response.data.account1.accountId. but if account1 is DYNAMIC how should i do it – Asim Nov 14 '17 at 13:00
  • or i can actually use this. Get the keys then iterate with the same variable from object.keys? but maby this is a performance issue? – Asim Nov 14 '17 at 13:03
  • Bracket notation for dynamic keys: `response.data[account1].accountId` where say `account1 = "Account 1"` – dfsq Nov 14 '17 at 13:09
  • @dfsq where did you get the [account1] from? by iterating keys first? – Asim Nov 14 '17 at 13:10
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – cнŝdk Nov 14 '17 at 13:11
  • 1
    yes, `Object.keys(obj).forEach(key => console.log(obj[key]))` – dfsq Nov 14 '17 at 13:12
  • Yes got it working now, using for loop to iterate keys then adding this inside a scope for then access value based on the key in for loop :) yeeah! i won 1million? – Asim Nov 14 '17 at 13:14
  • Still getting wrong values out. Iterating right but i get only response of "account1" not "account", looks like it makes two requests. look updated question – Asim Nov 14 '17 at 13:19

1 Answers1

1

You need to use Object.keys() to get the keys of your object, then for each key get the relevant array and loop over its items:

Object.keys(data).forEach(function(key) {
  let accounts = data[key];
  if (accounts && accounts.length) {
    accounts.forEach(function(account) {
      console.log(account);
    });
  }
});

Demo:

let data = {
  "Account2": [{
    "accountId": "17467****",
    "containerId": "7454***",
    "path": "accounts\/17467*****\/containers\/7454***",
  }, {
    "accountId": "17467****",
    "containerId": "7519***",
    "path": "accounts\/17467****\/containers\/7519***",
  }],
  "Account 1": [{
    "accountId": "17661****",
    "containerId": "7483***",
    "path": "accounts\/17661****\/containers\/7483***",
  }]
};

Object.keys(data).forEach(function(key) {
  let accounts = data[key];
  if (accounts && accounts.length) {
    console.log(key+": ");
    accounts.forEach(function(account) {
      console.log(account);
    });
  }
});
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • I like how you mark it as duplicate then post an answer anyway :) Anyway, good answer. – Adrian Nov 14 '17 at 13:38
  • @Adriani6 This was before 1/2 hour, I saw that nobody wanted to close it as a dup, and he is still struggling to get it to work, so I decided to answer it :) – cнŝdk Nov 14 '17 at 13:39