0

This could be a pretty basic question, in JavaScript, what's difference between:

var userDetails = {};  
var usersList = [];  

I was reading an article which had following code:

 function GetSampleUsersList() {  
    var userDetails = {};  
    var usersList = [];  
    for (var i = 1; i <= 3; i++) {  
        userDetails["UserId"] = i;  
        userDetails["UserName"] = "User- " + i;  
        userDetails["Company"] = "Company- " + i;  
        usersList.push(userDetails);  
    }  
    return JSON.stringify(usersList);  
}  

Thanks

1
  • 1
    userDetails = {} is not an array and userDetails["UserId"] is just a bracket notation of accessing object's property Commented Sep 1, 2016 at 7:20

4 Answers 4

2

This is a pretty basic question.

var o = {}

initializes an empty object. Then you assign its properties.

 var a = []

initializes an empty array. You then add the newly created object to the array

a.push( o );
Sign up to request clarification or add additional context in comments.

Comments

2

You are using for every iteration the same userDetails, because you overwrite just the properties and while you have pushed the same object, you have always the same content for every element in the array.

 function GetSampleUsersList() {  
    var userDetails = {};
    var usersList = [];
    for (var i = 1; i <= 3; i++) {
        userDetails["UserId"] = i;
        userDetails["UserName"] = "User- " + i;
        userDetails["Company"] = "Company- " + i;
        usersList.push(userDetails);
    }
    return JSON.stringify(usersList);
}

console.log(GetSampleUsersList());

Better use a new empty object for every loop.

 function GetSampleUsersList() {  
    var userDetails;
    var usersList = [];
    for (var i = 1; i <= 3; i++) {
        userDetails = {}; // this is necessary
        userDetails["UserId"] = i;
        userDetails["UserName"] = "User- " + i;
        userDetails["Company"] = "Company- " + i;
        usersList.push(userDetails);
    }
    return JSON.stringify(usersList);
}
console.log(GetSampleUsersList());

Comments

0
function GetSampleUsersList() {      
var userDetails = {};  //created an empty object userDetails 
var usersList = [];  //created an empty array userDetails
for (var i = 1; i <= 3; i++) {  //looping to add property and value in object and for each iteration object is getting pushed into array at an index i.
    userDetails["UserId"] = i;  
    userDetails["UserName"] = "User- " + i;  
    userDetails["Company"] = "Company- " + i;  
    usersList.push(userDetails);  // pushing object {"UserId":i, "UserName":"User-i", "Company":"Company-i"} into array
}  
    return JSON.stringify(usersList);  // Parsing the object into javascript string
}  

Comments

0
 var userDetails = {}; -- object notation in javascript
 var usersList = [];  is an array notation in javascript.

for more infomation refer here http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".

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.