复杂选择和表格

时间:2019-02-04 12:34:38

标签: angular typescript

我有一个名为listSelected的数组,该数组在每次选择后都在另一个网格中提供。

const listSelected =    
 [{    
    "_id": "10",    
    "age": 35,    
    "name": "Paige Zamora",    
    "gender": "female",    
    "company": "AUTOMON",    
    "**reference_id": "12**"    
  },    
  {    
    "_id": "11",    
    "age": 40,    
    "name": "Jennifer Carr",    
    "gender": "female",    
    "company": "SYNKGEN",    
    "**reference_id": "11**"    
  }];  

我想检查每个值reference_id是否都在另一个名为data的表中进行检查,如果存在此值,即使在新数组中,我们也会得到值idreference_id存在多次。

const data = [{    
    "_id": "**10**",    
    "age": 35,    
    "name": "Paige Zamora",    
    "gender": "female",    
    "company": "AUTOMON",    
    "**reference_id": "12**"    
  },    
  {    
    "_id": "**11**",    
    "age": 40,    
    "name": "Jennifer Carr",    
    "gender": "female",    
    "company": "SYNKGEN",    
    "**reference_id": "11**"    
  },    
  {    
    "_id": "**12**",    
    "age": 38,    
    "name": "Weaver Rosales",    
    "gender": "male",    
    "company": "ETERNIS",    
    "**reference_id": "12**"    
  },    
  {
    "_id": "13",    
    "age": 31,    
    "name": "Myers Pickett",    
    "gender": "male",    
    "company": "ETERNIS",    
    "reference_id": "13"    
  },    
  {    
    "_id": "14",    
    "age": 36,    
    "name": "Dona Nicholson",    
    "gender": "female",    
    "company": "ETERNIS",    
    "reference_id": "14"    
  },    
  {
    "_id": "15",    
    "age": 21,    
    "name": "Niki Blur",    
    "gender": "female",    
    "company": "AUTOMON",    
    "reference_id": "15"    
  },    
  {    
    "_id": "16",    
    "age": 37,    
    "name": "Bod Dennart",    
    "gender": "male",    
    "company": "SYNKGEN",    
    "reference_id": "16"    
  },    
  {    
    "_id": "17",    
    "age": 26,    
    "name": "Richard Nelson",    
    "gender": "male",    
    "company": "ETERNIS",    
    "reference_id": "17"    
  },    
  {    
    "_id": "**12**",    
    "age": 45,    
    "name": "Pedro Kluivert",    
    "gender": "female",    
    "company": "SYNKGEN",    
    "**reference_id": "18**"    
  }    
];

最后,新网格将提供以下数据:

const newGrid = [11,12]

我当时正在考虑使用两个嵌套的forEach函数,如下所示:

const newGrid = [];

listSelected.forEach((elt) => {
  data.forEach((item) => {
    if (item.reference_id === elt.reference_id) {
       newGrid.push(item.id);
       newGrid = Array.from(new Set(newGrid));
     }
   });
});

是否有一种更简单的方法来简化此功能,以避免使用嵌套的forEach

3 个答案:

答案 0 :(得分:3)

您可以创建一个“反向唯一”数组:

const listSelected = [
  { "_id": "10", "age": 35, "name": "Paige Zamora", "gender": "female", "company": "AUTOMON", "reference_id": "12" }, 
  { "_id": "11", "age": 40, "name": "Jennifer Carr", "gender": "female", "company": "SYNKGEN", "reference_id": "11" }
];

const data = [
  { "_id": "10", "age": 35, "name": "Paige Zamora", "gender": "female", "company": "AUTOMON", "reference_id": "12" }, 
  { "_id": "11", "age": 40, "name": "Jennifer Carr", "gender": "female", "company": "SYNKGEN", "reference_id": "11" }, 
  { "_id": "12", "age": 38, "name": "Weaver Rosales", "gender": "male", "company": "ETERNIS", "reference_id": "12" }, 
  { "_id": "13", "age": 31, "name": "Myers Pickett", "gender": "male", "company": "ETERNIS", "reference_id": "13" }, 
  { "_id": "14", "age": 36, "name": "Dona Nicholson", "gender": "female", "company": "ETERNIS", "reference_id": "14" }, 
  { "_id": "15", "age": 21, "name": "Niki Blur", "gender": "female", "company": "AUTOMON", "reference_id": "15" }, 
  { "_id": "16", "age": 37, "name": "Bod Dennart", "gender": "male", "company": "SYNKGEN", "reference_id": "16" }, 
  { "_id": "17", "age": 26, "name": "Richard Nelson", "gender": "male", "company": "ETERNIS", "reference_id": "17" }, 
  { "_id": "12", "age": 45, "name": "Pedro Kluivert", "gender": "female", "company": "SYNKGEN", "reference_id": "18" }
];

const merged = [
  ...listSelected.map(item => item.reference_id),
  ...data.map(item => item.reference_id)
];

const reverseUnique = merged.filter((item, index, array) => array.indexOf(item) === index && array.lastIndexOf(item) !== index);

console.log(reverseUnique);

这只会产生一个循环,而不是其他所有方法都嵌套两个(因为是的,filter/find/some在数组上循环)

首先将两个数组串联在一起,然后仅使用引用ID。

然后,您使用the opposite of a unique filtering function

答案 1 :(得分:-1)

您可以使用数组的find方法,如下所示:

newGrid = [];

listSelected.forEach((elt) => {

    newGrid.push(data.find(function check(item) {

        return item.reference_id === elt.reference_id;

    }).reference_id);
});

答案 2 :(得分:-1)

const newGrig = listSeleced.map(listItem => {
    if (data.some(dataItem => dataItem.item.reference_id === listItem.item.reference_id) {
        return listItem.reference_id;
    }
    return;
});

尚未对此进行测试,但它应该可以工作。您还可以使用reduce进行类似的操作。