Javascript array of objetcs

Hello !

I’m coding a game and I have a strange problem … I store in mongodb a document (I think this is the correct word for mongodb) named PlayerEmployees. In this document I have an object named employees and in this object I have some key/value pairs :

    "_id": 152,
		"Software Developper": {"idEmployee": 1, "name": "Software Developper", "indexTable": 1, "quantity": 1, "currentCost": 560.00, "currentIncomeRate": 10, "display": "block"},
		"Designer": {"idEmployee": 2, "name": "Designer", "indexTable": 2, "quantity": 34, "currentCost": 560.00, "currentIncomeRate": 10, "display": "none"}

As you can see my key/value are simple. I have the name of the employee in key and and objet which describes it (name, quantity, etc).
I want to display on screen these informations in a template. To do that I have a helper : 

    getEmployees: function() {
    		var currentUser = Meteor.user();
    		var objEmployees = PlayerEmployees.findOne({_id: currentUser.playerEmployeesId}).employees;//getPlayerEmployees(Meteor.user());
    		var tabEmployees = [];

		// On regarde pour chaque employé invisible si le joueur a assez d'argent pour les débloquer
		for (var key in objEmployees) {


As you can see I must transform a little bit my objEmployees to send it to the template, I must send an array of object.I want, for each key/value, take the value which is an object and put it in my tabEmployee and after send tabEmployees to the template.

For me it was really simple but it doesn't work .... The result is an array of array ob object .... You can see the result on this consol screen : 
<img src="/uploads/default/original/2X/6/67f140521df01bb5ac9ece056d0f86ce768dab8e.JPG" width="690" height="68"> 

I don't understand why ! 
I can fix this bug if in my loop I write : `tabEmployees.push(objEmployees[key][0]);`

several problems here:

  1. You are printing the object, while you want to print the object value. So you either do something like:


or define a custom method toString that print a correct representation of the object (be it text, HTML or whatever) and then use it.

  1. You don’t want to store array of objects in a mongodb collection. It’s better to use a separate collection. Search for data denormalization.
1 Like

I don’t understand your 2nd point, how can I store informations about employee if I can’t store objet or array ?

Sure you can! But first you have to think over the collection of documents structure (to do it mongoDB way >> read about denormalization)

1 Like