简体   繁体   English

Javascript-从另一个数组中按值查找子数组

[英]Javascript - Finding in subarray by value from another array

I have two javascript objects: 我有两个JavaScript对象:

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. 好的,我需要创建一个遍历classroom变量中每个student每个item的函数。 With each item , I need to find in the items array the object that has the exact same key in one of its tags . 对于每个item ,我需要在items数组中找到在其tags之一中具有完全相同的key的对象。

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. 我知道foreach是一种指针,但是我真的不明白为什么它会奇怪地工作以及应该如何做。

Regards, 问候,

javascript variables only save object references, not the actual object in memory, so this line: javascript变量仅保存对象引用,而不保存内存中的实际对象,因此此行:

var newitem = theitem;

means newitem refers to the same object as theitem, NOT create a new object from theitem. 表示newitem与theitem引用同一对象,而不是从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: 要获得所需的行为,您需要创建theitem的副本并将该对象分配给newitem变量:

var newitem = Object.create(theitem);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM