Sami
Sami

Reputation: 3800

JavaScript arrays declaration ways difference

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

Upvotes: 0

Views: 57

Answers (4)

Harish Nayak
Harish Nayak

Reputation: 348

 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/

Upvotes: 0

Rahul Pandey
Rahul Pandey

Reputation: 445

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
}  

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

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());

Upvotes: 2

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

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

Upvotes: 2

Related Questions