1

I'm wondering if it is possible to create a javascript map with array of objects as content.

My code is as follows:

//Object that replicates a Map
var itemRcptMap = {};

//Receipt Object Holder
var objReceipt = {
     id     : '',
     item   : '',
     qty    : '',
     lotNo  : ''
};

//Iterate an object result w/c is not shown in the example 
for (var i in itemRcptResult)
{
    var id = itemRcptResult[i].id;

    if(!itemRcptMap[id]){
        itemRcptMap[id] = new Array();
    }
    objReceipt.id    = arItemRcptResult[i]id;
    objReceipt.item  = arItemRcptResult[i].items;
    objReceipt.qty   = arItemRcptResult[i].quantity;
    objReceipt.lotNo = arItemRcptResult[i].test;
    itemRcptMap[id] = itemRcptMap[id].push(objReceipt);
}

I'm having the following error

Cannot find function push in object 1.

How can I correct this?

THank you.

Code Simplification:

var itemRcptMap = {};

//Receipt Object Holder
var objReceipt = {
     id     : '',
     item   : '',
     qty    : '',
     lotNo  : ''
};

var id = 5555;
    if(!itemRcptMap[id]){
        itemRcptMap[id] = new Array();
    }
    objReceipt.id    = 1;
    objReceipt.item  = 2;
    objReceipt.qty   = 3;
    objReceipt.lotNo = 4;
    itemRcptMap[id].push(objReceipt);

 //Gets the object with Key 5555
var x = itemRcptMap[5555];  
alert(x.id)
1
  • var itemRcptMap = new Array(); Commented Aug 12, 2014 at 13:42

1 Answer 1

4

When you push to an array, the return value will be the new length of the array (which you are reassigning back to the actual array) so in the next iteration you are not pushing it to the array instead you are trying to access push method of numeric value(number does not have a push method.). Push mutates the source array so just do:-

itemRcptMap[id].push(objReceipt);

You might want to move objReceipt declaration inside of the loop. Other wise you will end up getting the same object in all the arrays.

Based on your comments, possibly you are looking to create a flat map with id (not duplicated) you could do.

//Object that replicates a Map
var itemRcptMap = {};

for (var i=0, l=itemRcptResult.length; i<l; i++){ 
    var id = itemRcptResult[i].id;
    itemRcptMap[id] = itemRcptResult[i];
}

Note if itemRcptResult is an array then dont use for..in loop

Sign up to request clarification or add additional context in comments.

4 Comments

It still does not work... see my edit for the simplification of my code
i mean, it works but i still cannot access the object. I've edited my post
@newbie i dont know what are you trying to do , but whatever you are doing you need to do var x = itemRcptMap[5555][0]; to access the object. Because itemRcptMap[id] is an array which has the the object that you are looking for. One more thing i would recommend is to prefer using [] over new Array();
@newbie are you looking for map?, then don't use push instead just do itemRcptMap[id] = objReceipt; provided you dont have id duplicated

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.