Javascript - 通过另一个数组中的值查找子数组

时间:2016-04-21 18:33:44

标签: javascript arrays

我有两个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"
  }
];

好的,我需要创建一个函数,该函数遍历item变量中每个student的{​​{1}}个classroom。对于每个item,我需要在items数组中找到其中一个key中具有完全相同tags的对象。

我的代码得到了奇怪的结果......不匹配的项目......

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是一种指针,但我不太明白为什么它工作奇怪以及应该怎么做。

此致

2 个答案:

答案 0 :(得分:2)

javascript变量只保存对象引用,而不是内存中的实际对象,所以这一行:

var newitem = theitem;

表示newitem引用与theitem相同的对象,而不是从theitem创建新对象。

所以

newitem.tag = obj;

相同
theitem.tag = obj;

这意味着您正在修改输入对象,这就是您无法获得预期输出的原因。

要获得所需的行为,您需要创建该项的副本并将该对象分配给newitem变量:

var newitem = Object.create(theitem);

答案 1 :(得分:1)

也许这有助于进行更多的迭代。



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>');
&#13;
&#13;
&#13;