如何根据另一个对象数组查找对象数组?

时间:2019-07-18 18:49:33

标签: javascript ecmascript-6

如何基于input字段(如果该字段存在于user中)过滤filterArray数组的值并获得expectedOuput

const filterArray = [{
  user: "bcasey1",
  userfullname: "Bertha Casey"
}, {
  user: "admin1",
  userfullname: "Administrator 1"
}];

const input = [{
    "id": 133557,
    "user": "bcasey1",
    "userfullname": "Bertha Casey",
    "commentTypeId": 2,
    "annotationPrimaryId": 141614,
    "comment": "Red color on ravioli is not true, fix",
    "deleted": false,
    "historyno": "133557-0",
    "timestamp": "Tue Apr 24 10: 40: 42 CDT 2018",
    "type": "rectangle",
    "commentNum": 1
  },
  {
    "id": 134038,
    "user": "admin",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 1,
    "annotationPrimaryId": 142286,
    "comment": "test123",
    "deleted": false,
    "historyno": "134038-0",
    "timestamp": "Mon Jul 8 22: 15: 18 CDT 2019",
    "type": "rectangle",
    "commentNum": 2
  },
  {
    "id": 134039,
    "user": "admin1",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 2,
    "annotationPrimaryId": 142287,
    "comment": "test234",
    "deleted": false,
    "historyno": "134039-0",
    "timestamp": "Mon Jul 8 22: 15: 35 CDT 2019",
    "type": "rectangle",
    "commentNum": 3
  },
  {
    "id": 134112,
    "user": "admin",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 3,
    "annotationPrimaryId": 142361,
    "comment": "sadasdasd",
    "deleted": false,
    "historyno": "134112-0",
    "timestamp": "Wed Jul 17 13: 03: 55 CDT 2019",
    "type": "rectangle",
    "commentNum": 4
  },

];

const expectedOuput = [{
    "id": 133557,
    "user": "bcasey1",
    "userfullname": "Bertha Casey",
    "commentTypeId": 2,
    "annotationPrimaryId": 141614,
    "comment": "Red color on ravioli is not true, fix",
    "deleted": false,
    "historyno": "133557-0",
    "timestamp": "Tue Apr 24 10: 40: 42 CDT 2018",
    "type": "rectangle",
    "commentNum": 1
  },
  {
    "id": 134039,
    "user": "admin1",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 2,
    "annotationPrimaryId": 142287,
    "comment": "test234",
    "deleted": false,
    "historyno": "134039-0",
    "timestamp": "Mon Jul 8 22: 15: 35 CDT 2019",
    "type": "rectangle",
    "commentNum": 3
  },
];

console.log('expectedOuput',expectedOuput);

3 个答案:

答案 0 :(得分:2)

您可以根据filterArray中的some()个用户是否共享用户属性进行过滤。

const expectedOuput = input.filter(p => filterArray.some(user => user.user == p.user))

这每次都通过filterArray进行浏览,效率不高。如果列表很大,则值得将filterArray中的元素提取到某种哈希中,以便更快地进行搜索。

const filterArray = [{
  user: "bcasey1",
  userfullname: "Bertha Casey"
}, {
  user: "admin1",
  userfullname: "Administrator 1"
}];



const input = [{
    "id": 133557,
    "user": "bcasey1",
    "userfullname": "Bertha Casey",
    "commentTypeId": 2,
    "annotationPrimaryId": 141614,
    "comment": "Red color on ravioli is not true, fix",
    "deleted": false,
    "historyno": "133557-0",
    "timestamp": "Tue Apr 24 10: 40: 42 CDT 2018",
    "type": "rectangle",
    "commentNum": 1
  },
  {
    "id": 134038,
    "user": "admin",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 1,
    "annotationPrimaryId": 142286,
    "comment": "test123",
    "deleted": false,
    "historyno": "134038-0",
    "timestamp": "Mon Jul 8 22: 15: 18 CDT 2019",
    "type": "rectangle",
    "commentNum": 2
  },
  {
    "id": 134039,
    "user": "admin1",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 2,
    "annotationPrimaryId": 142287,
    "comment": "test234",
    "deleted": false,
    "historyno": "134039-0",
    "timestamp": "Mon Jul 8 22: 15: 35 CDT 2019",
    "type": "rectangle",
    "commentNum": 3
  },
  {
    "id": 134112,
    "user": "admin",
    "userfullname": "Administrator Administrator",
    "commentTypeId": 3,
    "annotationPrimaryId": 142361,
    "comment": "sadasdasd",
    "deleted": false,
    "historyno": "134112-0",
    "timestamp": "Wed Jul 17 13: 03: 55 CDT 2019",
    "type": "rectangle",
    "commentNum": 4
  },

];

const expectedOuput = input.filter(p => filterArray.some(user => user.user == p.user))

console.log('expectedOuput',expectedOuput);

答案 1 :(得分:1)

创建Set的用户名以进行过滤。...然后Array#filter()检查用户是否在该集合中。

const users = new Set( filterArray.map(({user}) => user))

const res = input.filter(({user}) => users.has(user))

console.log(res)
<script>
  const filterArray = [{
    user: "bcasey1",
    userfullname: "Bertha Casey"
  }, {
    user: "admin1",
    userfullname: "Administrator 1"
  }];

  const input = [{
      "id": 133557,
      "user": "bcasey1",
      "userfullname": "Bertha Casey",
      "commentTypeId": 2,
      "annotationPrimaryId": 141614,
      "comment": "Red color on ravioli is not true, fix",
      "deleted": false,
      "historyno": "133557-0",
      "timestamp": "Tue Apr 24 10: 40: 42 CDT 2018",
      "type": "rectangle",
      "commentNum": 1
    },
    {
      "id": 134038,
      "user": "admin",
      "userfullname": "Administrator Administrator",
      "commentTypeId": 1,
      "annotationPrimaryId": 142286,
      "comment": "test123",
      "deleted": false,
      "historyno": "134038-0",
      "timestamp": "Mon Jul 8 22: 15: 18 CDT 2019",
      "type": "rectangle",
      "commentNum": 2
    },
    {
      "id": 134039,
      "user": "admin1",
      "userfullname": "Administrator Administrator",
      "commentTypeId": 2,
      "annotationPrimaryId": 142287,
      "comment": "test234",
      "deleted": false,
      "historyno": "134039-0",
      "timestamp": "Mon Jul 8 22: 15: 35 CDT 2019",
      "type": "rectangle",
      "commentNum": 3
    },
    {
      "id": 134112,
      "user": "admin",
      "userfullname": "Administrator Administrator",
      "commentTypeId": 3,
      "annotationPrimaryId": 142361,
      "comment": "sadasdasd",
      "deleted": false,
      "historyno": "134112-0",
      "timestamp": "Wed Jul 17 13: 03: 55 CDT 2019",
      "type": "rectangle",
      "commentNum": 4
    },

  ];
</script>

答案 2 :(得分:0)

返回对象:

function filterByUserType(userType) {
  return input.filter((item) => {
    return (item.user === userType)
  })
}

console.log(filterByUserType('bcasey1'));

要查看它是否存在:

function hasUserType(userType) {
  return input.some((item) => {
    return (item.user === userType) ? true : false;
  })
}

console.log(hasUserType('bcasey1'));