SURYA VISWANATHAN
SURYA VISWANATHAN

Reputation: 187

Object is not defined - Javascript

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.

Upvotes: 0

Views: 844

Answers (3)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

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) {

Upvotes: 0

Sai
Sai

Reputation: 2658

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)}
    }
}

Upvotes: 1

ricky
ricky

Reputation: 1674

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))}
                }}

Upvotes: 0

Related Questions