0

I have two javascript objects:

var classroom = {
  "number" : "1",
    "student" : [ 
      {
        "number" : 1,
        "items" : [ 
          {
            "key" : "00000000000000000000001C",
            "date" : "2016-04-21T17:35:39.997Z"
          }
        ]
      }, 
      {
        "number" : 2,
        "items" : [ 
          {
            "key" : "00000000000000000000001D",
            "date" :"2016-04-21T17:35:39.812Z"
          }, 
          {
            "key" : "00000000000000000000002N",
            "date" :"2016-04-21T17:35:40.159Z"
          }, 
          {
            "key" : "00000000000000000000002Ñ",
            "date" :"2016-04-21T17:35:42.619Z"
          }
        ]
      }
    ],
  }

AND

var items = [ 
  {
    "fields" : {
      "tags" : [ 
        {
          "key" : "00000000000000000000001C",
          "Batch" : "50",
          "Bin" : "01",
          "Tray" : "02"
        }, 
        {
          "key" : "00000000000000000000002N",
          "Batch" : "55",
          "Bin" : "05",
          "Tray" : "12"
        }, 
        {
          "key" : "000000000000228510000032",
          "Batch" : "12",
          "Bin" : "12",
          "Tray" : "01"
        }
      ],
      "Name" : "Rubber"
    },
    "_id" : "56d19b48faa37118109977c0"
  }, 
  {
    "fields" : {
      "tags" : [ 
        {
          "key" : "00000000000000000000001D",
          "Batch" : "50",
          "Bin" : "01",
          "Tray" : "05"
        }, 
        {
          "key" : "00000000000000000000002Ñ",
          "Batch" : "52",
          "Bin" : "07",
          "Tray" : "02"
        }, 
        {
          "key" : "221567010000000000000089",
          "Batch" : "11",
          "Bin" : "15",
          "Tray" : "03"
        }
      ],
      "Name" : "Book"
    },
    "_id" : "56d19b48faa37118109977c1"
  }
];

Ok, I need to create a function that goes through every item of every student in classroom variable. With each item, I need to find in the items array the object that has the exact same key in one of its tags.

My code is getting strange results...missmatching items...

var finalitems = [];

classroom.student.forEach( function (student){
  student.items.forEach( function (obj){

    items.forEach( function (theitem){
      theitem.fields.tags.forEach( function (tag){

        if (tag.key === obj.key) {

          var newitem = theitem;
          newitem.tag = obj;
          finalitems.push(newitem);      
        }
      });
    });         
  });
});

I know that foreach is a kind of a pointer but I don't really understand why it is working strange and how it should be done.

Regards,

3
  • Create a working snippet. Define "strange results". Commented Apr 21, 2016 at 18:38
  • jsfiddle.net/ynqtjL3d Please, find that the tag of each item is not what it should be. I can see 4 items (correct) but there are two items with tag key "00000000000000000000002Ñ" and another two with tag key "00000000000000000000002N" when there should be 4 items but each one with a different tag key (00000000000000000000001C,00000000000000000000001D,00000000000000000000002N,00000000000000000000002Ñ) Commented Apr 21, 2016 at 18:42
  • please add how the result should look like. Commented Apr 21, 2016 at 18:53

2 Answers 2

2

javascript variables only save object references, not the actual object in memory, so this line:

var newitem = theitem;

means newitem refers to the same object as theitem, NOT create a new object from theitem.

so

newitem.tag = obj;

is the same as

theitem.tag = obj;

Which means you're modifying the input objects, that's why you won't get the expected output.

To get the desired behavior you need to create a copy of theitem and assign that object to newitem variable:

var newitem = Object.create(theitem);
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe this helps with a lot more iterations.

var classroom = { "number": "1", "student": [{ "number": 1, "items": [{ "key": "00000000000000000000001C", "date": "2016-04-21T17:35:39.997Z" }] }, { "number": 2, "items": [{ "key": "00000000000000000000001D", "date": "2016-04-21T17:35:39.812Z" }, { "key": "00000000000000000000002N", "date": "2016-04-21T17:35:40.159Z" }, { "key": "00000000000000000000002Ñ", "date": "2016-04-21T17:35:42.619Z" }] }] },
    items = [{ "fields": { "tags": [{ "key": "00000000000000000000001C", "Batch": "50", "Bin": "01", "Tray": "02" }, { "key": "00000000000000000000002N", "Batch": "55", "Bin": "05", "Tray": "12" }, { "key": "000000000000228510000032", "Batch": "12", "Bin": "12", "Tray": "01" }], "Name": "Rubber" }, "_id": "56d19b48faa37118109977c0" }, { "fields": { "tags": [{ "key": "00000000000000000000001D", "Batch": "50", "Bin": "01", "Tray": "05" }, { "key": "00000000000000000000002Ñ", "Batch": "52", "Bin": "07", "Tray": "02" }, { "key": "221567010000000000000089", "Batch": "11", "Bin": "15", "Tray": "03" }], "Name": "Book" }, "_id": "56d19b48faa37118109977c1" }],
    finalitems = [];

classroom.student.forEach(function (student) {
    student.items.forEach(function (studentItem) {
        items.forEach(function (item) {
            item.fields.tags.forEach(function (itemTag) {
                if (itemTag.key === studentItem.key) {
                    finalitems.push({
                        key: studentItem.key,
                        date: studentItem.date,
                        Batch: itemTag.Batch,
                        Bin: itemTag.Bin,
                        Tray: itemTag.Tray,
                    });
                }
            });
        });
    });
});
document.write('<pre>' + JSON.stringify(finalitems, 0, 4) + '</pre>');

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.