1

In a AngularJS environment I've got two JSON objects like the following:

var rowData = {
     fields: {
         Id: 1234,
         Code: xyz
     }
};
var access = {
    0: "fields['Id']"
}

now I want to access the value of rowData.fields['Id'] generic by evaluating the value of access[0].

I tried:

var result = rowData.access[0]

which of course does not work. Problem seems to be that the value of access[0] is a string.

How do I convert this to being usable in this given scenario?

3
  • I'll add this as a comment as I'm sure there must be a better way to do it... eval("rowData." + access[0]); Commented Mar 11, 2014 at 12:26
  • This is an unusual construct. What are you trying to do? Commented Mar 11, 2014 at 12:41
  • @ken4z: "fields['Id']" is the kind of structure ng-grid uses to render it's grid. Im trying to use this data-structure to build a separate card-view, which can be used for mobile environments. You get the data unsorted and have to sort it by a structure defined in a column-json. This is why it's gettin kind of acrobatic in here... ;) Commented Mar 11, 2014 at 13:32

1 Answer 1

3

Since you want this in the context of Angular, you can utilize the $parse service (ref):

var getter = $parse(access[0]);
var result = getter(rowData);
Sign up to request clarification or add additional context in comments.

Comments

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.