1

Is there any way to read an object from a list of objects dynamically with object key. My complex objects is something like :

$scope.mainObject = {
   name : "Some value",
   desc : "Some desc",
   key1: {
      arrayLst: []
   },
   key2: {
      arrayLst: []
   }
}

In my method, I have the key value either key1 or key2 in a string keyValue. How can I write to object like :

$scope.mainObject.key1.arrayLst = data;

In the form of something like :

$scope.mainObject.get(keyValue).arrayLst = data;
1
  • use bracket syntax, i.e. $scope.mainObject[keyValue].arrayLst Commented Sep 16, 2015 at 4:19

1 Answer 1

1

Well, there's something known as Array notation in JavaScript objects. More on it here.

You can write it something like this :

$scope.mainObject.[keyValue].arrayLst = data;
Sign up to request clarification or add additional context in comments.

5 Comments

it's called Bracket Notation; even though it uses the same characters [ ] it does not imply that the data is an Array.
Well, i didn't said that it's an array.. Since the approach of accessing the data is quite similar to Arrays, the terminology is so..
OP can even use $scope.mainObject["key1.arrayLst[]"] ie square bracket notation
@ManishKr.Shukla In my opinion OP has already been declared arrayLst as an array so you should suggest him to push the data in arrayLst.
@ManishKr.Shukla no, the exact opposite is true. The terminology is "Bracket Notation", or "Square Bracket Notation" specifically to ensure that there is no confusion with the Array data structure. The article you linked to even refers to it as Square Bracket Notation. just by using the word Array in your question, you introduced a suggested comment that doesn't even really make sense about how to manage arrays.

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.