Javascript - Finding in subarray by value from another array -


i have 2 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, need create function goes through every item of every student in classroom variable. each item, need find in items array object has exact same key in 1 of tags.

my code 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 foreach kind of pointer don't understand why working strange , how should done.

regards,

javascript variables save object references, not actual object in memory, line:

var newitem = theitem; 

means newitem refers same object theitem, not create new object theitem.

so

newitem.tag = obj; 

is same as

theitem.tag = obj; 

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

to desired behavior need create copy of theitem , assign object newitem variable:

var newitem = object.create(theitem); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -