0

I have a list in which I need get the value depending on certain calculations. Check out the below code for more understanding.

 $scope.allcolors = [
                            { id:1, color:'#67b05c' },
                            { id:2, color:'#7dc47a'},
                            { id:3, color:'#b4cf8a' },
                            { id:4, color:'#c7c354'},
                            { id:5, color:'#e5e154' },
                            { id:6, color:'#e5e154'},
                            { id:7, color:'#eb9d54' },
                            { id:8, color:'#db8a42'},
                            { id:9, color:'#cf9373' },
                            { id:10, color:'#db755e'}
                            ];
    if(r_header.severity){
                console.log("sev", r_header.severity);
                for (var i in $scope.allcolors) {
                    if ($scope.allcolors[i].id == r_header.severity) {
                        console.log("color", object.values($scope.allcolors[i]))}
                }}

r_header.severity is an integer variable which will either have values from 1 to 10. Depending on this value, I need to get color specified in the list. The above code flags Object not defined error. I checked for similar references but nothing seems to be appropriate with my case.

4
  • 2
    object will be Object. Commented Jan 4, 2017 at 6:59
  • Typo error console.log("color", Object.values($scope.allcolors[i]))} Commented Jan 4, 2017 at 7:01
  • Still the same. Commented Jan 4, 2017 at 7:01
  • 1
    i think you can write this alone "console.log("color", $scope.allcolors[i])" this will print you the object Commented Jan 4, 2017 at 7:02

3 Answers 3

1

Change the for loop:

for (var i=0; i<$scope.allcolors.length; i++) {
    if ($scope.allcolors[i].id == r_header.severity) {
        console.log("color", $scope.allcolors[i].color)}
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In for loop each iteration will return the object of array in i means 'i = Object("id","color")'

 $scope.allcolors = [
                            { id:1, color:'#67b05c' },
                            { id:2, color:'#7dc47a'},
                            { id:3, color:'#b4cf8a' },
                            { id:4, color:'#c7c354'},
                            { id:5, color:'#e5e154' },
                            { id:6, color:'#e5e154'},
                            { id:7, color:'#eb9d54' },
                            { id:8, color:'#db8a42'},
                            { id:9, color:'#cf9373' },
                            { id:10, color:'#db755e'}
                            ];
    if(r_header.severity){
                console.log("sev", r_header.severity);
                for (var i in $scope.allcolors) {
                    if (i.id == r_header.severity) {
                        console.log("color", i.color))}
                }}

Comments

0

try this if you want to see all object

console.log("color", JSON.stringify($scope.allcolors[i]))}

and

for (var i=0; i<$scope.allcolors.length; i++) {

instead of

console.log("color", object.values($scope.allcolors[i]))}

and

for (var i in $scope.allcolors) {

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.