1

I have an object that looks like this. Basically what I need is to loop through the filterNameList array and pull out the first value in each of the objects that belong to the filterNameList, it will always be the [0] value. Having some trouble getting these extracted out to display on the page.

acceptableFlag: "True"
filterNameList: Array[5]
    0: Array[2]
        0: "Asia"
        1: 20000
    1: Array[2]
        0: "China"
        1: 20001
    2: Array[2]
        0: "Beijing"
        1: 50000
reportId: 48
reportName: "Multiple"

Would the following function work?

function filterLabel(data){
    angular.forEach(data, function(obj, i){
        angular.forEach(obj, function(filterNameList, i){
            if (filterNameList.filterNameList == 20000){
                filterNameList.filterLabel = "Region"
            }
        })
    })
 };
2
  • Why do you use angular.forEach as opposed to data.filterNameList.forEach(function(nameList, i) {}) Commented Nov 24, 2015 at 20:35
  • throughout other places in the code i've seen angular.forEach being used, so I adopted it also Commented Nov 24, 2015 at 20:41

2 Answers 2

3

obj is an array as well. You'll either have to nest a loop, or do a static index value.

if(obj[1] == 20000){
    obj.regionName = "Region"
}
Sign up to request clarification or add additional context in comments.

Comments

1

Assigning property to an Array object is not a good way of doing this. You can do something like

filterNameList.forEach(function(obj, i){
   if(obj[1] == 20000){
        obj.push("Region")
    }
   else if (obj[1] == 20001){
       obj.push('Country');
   }
   else if (obj[1] == 50000){
       obj.push('City');
   }

});

5 Comments

so would it be like this: function filterLabel(data.filterNameList){ filterNameList.forEach(function(obj, i){ if.... } }
Right.. after this your sub arrays will contain 3 items each
I am getting an error, saying "Not a function / got undefined"
Maybe there is a typo or filterNamesList is undefined
ok thanks, i got it fixed, but I actually edited the question above

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.