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"

11
  • 1
    Use Object.keys() ? Iterate over all the keys? Commented Nov 14, 2017 at 12:45
  • @Adriani6 console.log(Object.keys(response.data)); gives me "account1" and "account2" not the key:value itself Commented Nov 14, 2017 at 12:53
  • 2
    @Asim Question for 1 million: if you have object and a key, how to access value for this key? Commented Nov 14, 2017 at 12:59
  • 1
    Possible duplicate of Access / process (nested) objects, arrays or JSON Commented Nov 14, 2017 at 13:11
  • 1
    yes, Object.keys(obj).forEach(key => console.log(obj[key])) Commented Nov 14, 2017 at 13:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I like how you mark it as duplicate then post an answer anyway :) Anyway, good answer.
@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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.