Question 1
In my program, I am receiving a JSON object called scope.tagSet
, which has the structure shown below.
{ Tags : [
{"TagID" : "ID1" , "TagName" : "Name1"},
{"TagID" : "ID2" , "TagName" : "Name2"},
{"TagID" : "ID3" , "TagName" : "Name3"}
]
}
Within an angular directive, I am creating an array of all the values in TagName
like this.
for(var i= 0; i < scope.tagSet.Tags.length; i++){
scope.tagNames[i] = scope.tagSet.Tags[i].TagName;
}
Is there only one method to iterate through and assign each value? Or is there a more efficient way to achieve this task?
Question 2
Now, if I have an array named tagNames[]
, my next task is to search for a specific variable varString
within the tagNames[]
array and return true or false based on its existence.
I understand that using
scope.tagNames.indexOf(varString)
will result in -1
if there are no matches,
but is this considered the appropriate angular approach? Are these methods generally viewed as best practices?