我有两个像这样的数组
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)
}
输出数组值不应该包含任何重复,我用于每个循环,是否有任何最佳实践来解决这种情况,如解除循环,因为上面的数组长度很多,如果我使用更多的循环,它会增加加载时间,请让我知道如何巧妙地解决这个问题
提前致谢
答案 0 :(得分:2)
使用标准map
,filter
和reduce
相当容易:
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);
答案 1 :(得分:1)
首先让我们过滤我们的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);

答案 2 :(得分:1)
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);
}