基于一个数组对象键和其他数组对象值再创建一个新数组?

Based on one array object keys and other array object values create one more new array?

我有两个像这样的数组

    arr1 = [  
   {  
      'name':'Victoria Cantrell',
      'position':'Integer Corporation',
      'office':'Croatia',
      'ext':'0839',
      'startDate':'2015-08-19',
      'salary':208.178
   },
   {  
      'name':'Pearleeee',
      'position':'In PC',
      'office':'Cambodia',
      'ext':'8262',
      'startDate':'2014-10-08',
      'salary':114.367
   },
   {  
      'name':'Pearl Crosby',
      'position':'Integer',
      'office':'Cambodia',
      'ext':'8162',
      'startDate':'2014-10-08',
      'salary':114.367
   }
]

arr2 = 

[{
  'name': 'name',
  'checkfilter': false
},
{
  'name': 'position',
  'checkfilter': true
},
{
  'name': 'office',
  'checkfilter': true
},
{
  'name': 'startDate',
  'checkfilter': false
},
{
  'name': 'ext',
  'checkfilter': false
},
{
  'name': 'salary',
  'checkfilter': false
}]

基于 checkfilter== true 我想像这样生成第三个数组

arr3 =`

[{
  name: 'position',
  values: [{
    checkName: 'Integer Corporation'

  },
  {
    checkName: 'In PC'

  },
  {
    checkName: 'Integer'

  }]
},

{ 
 name:'office',
  values: [{
    checkName: 'Croatia'

  },
  {
    checkName: 'Cambodia'

  }]
}
]

` 我试图像这样解决这种情况,但它并不完美

    arr3=[]
      this.arr2.forEach((column: any) => {
      if (column.ischeckFilter === true) {
        this.arr3.push({
          name: column.name,
          values: []
        });
      }
    });

 this.arr1.forEach((d: any) => {
      this.arr2.forEach((column: any) => {
        if (column.ischeckFilter === true) {
        this.arr3.forEach((c: any) => {
         // console.log(d[column.name], c.name, 'JJJJ');
         // console.log(Object.keys(d), 'BBBBBBBBB');
          let keys = Object.keys(d);
          keys.forEach((k: any) => {
            if (k === c.name) {
              if (find( c.values, { 'checkName': d[column.name]}) === undefined) {
                c.values.push({
                  checkName: d[column.name] ,
                  ischeck: false
                });
              }

            }
          });

      });
    }
      });
    });
    console.log( this.arr3)
  }

输出数组值不应包含任何重复项,我用于每个循环,是否有任何最佳实践来解决这种情况,例如减少循环,因为上面的数组长度如此之多,如果我使用更多循环,它会增加加载时间,所以请让我知道如何巧妙地解决这个问题

提前致谢

首先让我们过滤 arr2。

var x = arr2.filter((r) => r.checkfilter).

我们的名字被映射了..

map((r) => ({name:r.name, 

获取我们的匹配值。并删除重复值。

values:([...new Set(arr1.map((x) => (x[r.name])))]).

最后重新映射,所以我们得到 {checkName: ?}

map((r) => ({checkName:r}))

别忘了关闭括号和内容..

}));

const arr1 = [  
   {  
      'name':'Victoria Cantrell',
      'position':'Integer Corporation',
      'office':'Croatia',
      'ext':'0839',
      'startDate':'2015-08-19',
      'salary':208.178
   },
   {  
      'name':'Pearleeee',
      'position':'In PC',
      'office':'Cambodia',
      'ext':'8262',
      'startDate':'2014-10-08',
      'salary':114.367
   },
   {  
      'name':'Pearl Crosby',
      'position':'Integer',
      'office':'Cambodia',
      'ext':'8162',
      'startDate':'2014-10-08',
      'salary':114.367
   }
];

const arr2 = 

[{
  'name': 'name',
  'checkfilter': false
},
{
  'name': 'position',
  'checkfilter': true
},
{
  'name': 'office',
  'checkfilter': true
},
{
  'name': 'startDate',
  'checkfilter': false
},
{
  'name': 'ext',
  'checkfilter': false
},
{
  'name': 'salary',
  'checkfilter': false
}];

var x = arr2.filter((r) => r.checkfilter).
  map((r) => ({name:r.name, 
    values:([...new Set(arr1.map((x) => (x[r.name])))]).
      map((r) => ({checkName:r}))
  }));
  
console.log(x);

使用标准 mapfilterreduce 相当容易:

var arr1 = [{  
  'name':'Victoria Cantrell',
  'position':'Integer Corporation',
  'office':'Croatia',
  'ext':'0839',
  'startDate':'2015-08-19',
  'salary':208.178
 }, {  
  'name':'Pearleeee',
  'position':'In PC',
  'office':'Cambodia',
  'ext':'8262',
  'startDate':'2014-10-08',
  'salary':114.367
}, {  
  'name':'Pearl Crosby',
  'position':'Integer',
  'office':'Cambodia',
  'ext':'8162',
  'startDate':'2014-10-08',
  'salary':114.367
}];

const arr2 = [{
  'name': 'name',
  'checkfilter': false
},{
  'name': 'position',
  'checkfilter': true
},{
  'name': 'office',
  'checkfilter': true
},{
  'name': 'startDate',
  'checkfilter': false
},{
  'name': 'ext',
  'checkfilter': false
},{
  'name': 'salary',
  'checkfilter': false
}];

const isDuplicate = (arr, name) => !arr.find(({ checkname }) => checkname === name);
const arr3 = arr2
  .filter(({ checkfilter  }) => checkfilter)
  .map(({ name }) => ({
    name: name,
    values: arr1.reduce((names, item) =>  isDuplicate(names, item[name]) ?
      [...names, { checkname: item[name] } ] : names, [])
}));

console.log(arr3);

function createArr3(target, source) {
    for (var prop in source) {
        if (source[prop]["checkfilter"]) {
            var newArr3Element = {};
            newArr3Element.name = source[prop].name;
            newArr3Element.values = [];
            target.forEach(function(element) {
                if (newArr3Element.values.indexOf(element[source[prop].name]) < 0){
                    newArr3Element.values.push(element[source[prop].name]);
                }
            });
            arr3.push(newArr3Element);
        }
    }    
    console.log(arr3);
}

https://jsfiddle.net/x6140nct/