6

I have some code like the followings.

MyRequests.cors_request("POST", 
        APP_CONFIG.APP_URL+"/users/selectAllUsers", 
        null, 
        function ok(users) {  

            $scope.usersNotFiltered = users;

            console.log('users--->', users);
            console.log('$scope.userPerSystem--->', $scope.userPerSystem);

            // delete the items that is already exists in the userPerSystem
            function filterUsers(element, index, array) {

                console.log("$scope.usersNotFiltered :: " + users);
                commonFindAndRemove($scope.usersNotFiltered, 'userId', element.userId);
                console.log('a[' + index + '] = ' + element.userId);
            }

            $scope.userPerSystem.forEach(filterUsers);

            $scope.users = $scope.usersNotFiltered;   
}, 

And I run tihs code and watch the console. What is the difference between '[Object]' and '[object Object]' in chrome console?

users---> [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

$scope.userPerSystem---> [Object]0: Objectlength: 1__proto__: Array[0] redca-ias.concat.js:5258

$scope.usersNotFiltered :: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

1
  • what makes it difference? $scope.usersNotFiltered and users are same type's value that I returned value from $http callback. Commented May 4, 2015 at 8:07

1 Answer 1

7

The first log shows the structure as it is, i.e. the live representation of the structure and the second one is the string representation of the structure.

> var arr = [{}, {}, {}];
> arr // simply logs the object as it is
> [Object, Object, Object]
> arr + '' // converts the structure into string
> "[object Object],[object Object],[object Object]"

You get this result as you are concatenating a string with an object:

"$scope.usersNotFiltered :: " + users

In the above snippet JavaScript converts the structure into a string (a primitive value) and string representation of an object is [object Object]. Since the structure is an array of objects, string representation of each element is joined with ,. You get similar result by calling Array.prototype.toString() or Array.prototype.join() method:

> var arr = [1, 2, 3];
> arr.toString()
> "1,2,3"
> var arrayOfObjects = [{}, {}, {}];
> arrayOfObjects.toString()
> "[object Object],[object Object],[object Object]"
Sign up to request clarification or add additional context in comments.

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.