簡體   English   中英

Javascript-從另一個數組中按值查找子數組

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

我有兩個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"
          }
        ]
      }
    ],
  }

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"
  }
];

好的,我需要創建一個遍歷classroom變量中每個student每個item的函數。 對於每個item ,我需要在items數組中找到在其tags之一中具有完全相同的key的對象。

我的代碼得到了奇怪的結果...項目不匹配...

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

我知道foreach是一種指針,但是我真的不明白為什么它會奇怪地工作以及應該如何做。

問候,

javascript變量僅保存對象引用,而不保存內存中的實際對象,因此此行:

var newitem = theitem;

表示newitem與theitem引用同一對象,而不是從theitem創建新對象。

所以

newitem.tag = obj;

是相同的

theitem.tag = obj;

這意味着您正在修改輸入對象,這就是為什么您將無法獲得預期輸出的原因。

要獲得所需的行為,您需要創建theitem的副本並將該對象分配給newitem變量:

var newitem = Object.create(theitem);

也許這有助於進行更多的迭代。

 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