javascript对对象嵌套数组中的对象进行迭代,并按ID进行过滤,并返回相同的原始数据集

时间:2018-09-04 19:02:46

标签: javascript arrays ecmascript-6

我有一个嵌套对象数组,我想将items等于id的对象与我作为参考的过滤器列表中的对象保持一致。

const data = [{
  "otherStuff": "otherB",
  "items": {
    "item1": [{
        "id": "id1",
        "info": "info1"
      },
      {
        "id": "id2",
        "info": "info22"
      }
    ],
    "item20": [{
      "id": "id3",
      "info": "info5"
    }],
    "item5": [{
        "id": "id4",
        "info": "info6"
      },
      {
        "id": "id5",
        "info": "info7"
      }
    ]
  }
}, {
  "otherStuff": "otherA",
  "items": {
    "item1": [{
        "id": "id1",
        "info": "info10000"
      },
      {
        "id": "id2",
        "info": "info220000"
      }
    ],
    "item20": [{
      "id": "id3",
      "info": "info5000"
    }],
    "item5": [{
        "id": "id4",
        "info": "info60000"
      },
      {
        "id": "id5",
        "info": "info7000"
      }
    ]
  }
}];

const keep = ['id4', 'id2'];
keep.forEach(function(val) {
  data.forEach(function(entry, index){
      const entrySelected = [];
      Object.keys(entry.items).forEach(item => {
        var match = entry.items[item].find(obj => obj.id === val);
        if (match) {
          entrySelected.push(match)
        }
      });
      data[index].items = entrySelected;
  });
})
console.log(data)

我现在遇到一个错误, 但想法是得到以下输出:

 [
        {
            "otherStuff": "otherB",
            "items": [
                    {
                          "id": "id2",
                          "info": "info22"
                    },
                    {
                        "id": "id4",
                        "info": "info6"
                }
            ]
        },
        {
            "otherStuff": "otherA",
            "items": [
                    {
                          "id": "id2",
                          "info": "info220000"
                    },
                    {
                        "id": "id4",
                        "info": "info60000"
                }
            ]
        }
    ]

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以迭代对象的值,然后减少新数组的所需项。稍后将其关联到items

var data = [{ otherStuff: "otherB", items: { item1: [{ id: "id1", info: "info1" }, { id: "id2", info: "info22" }], item20: [{ id: "id3", info: "info5" }], item5: [{ id: "id4", info: "info6" }, { id: "id5", info: "info7" }] } }, { otherStuff: "otherA", items: { item1: [{ id: "id1", info: "info10000" }, { id: "id2", info: "info220000" }], item20: [{ id: "id3", info: "info5000" }], item5: [{ id: "id4", info: "info60000" }, { id: "id5", info: "info7000" }] } }],
    keep = ['id4', 'id2'];

data.forEach(o =>
    o.items = Object
        .keys(o.items)
        .reduce((r, k) => o
            .items[k]
            .reduce((s, p) => s.concat(keep.includes(p.id) ? p : []), r), []));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

带有用于数组的过滤器。

var data = [{ otherStuff: "otherB", items: { item1: [{ id: "id1", info: "info1" }, { id: "id2", info: "info22" }], item20: [{ id: "id3", info: "info5" }], item5: [{ id: "id4", info: "info6" }, { id: "id5", info: "info7" }] } }, { otherStuff: "otherA", items: { item1: [{ id: "id1", info: "info10000" }, { id: "id2", info: "info220000" }], item20: [{ id: "id3", info: "info5000" }], item5: [{ id: "id4", info: "info60000" }, { id: "id5", info: "info7000" }] } }],
    keep = ['id4', 'id2'];

data.forEach(o =>
    o.items = Object
        .keys(o.items)
        .filter(k => Array.isArray(o.items[k]))
        .reduce((r, k) => o
            .items[k]
            .reduce((s, p) => s.concat(keep.includes(p.id) ? p : []), r), []));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }