below is my function which have reduce method
getFilters(gridFilters, columnList){
const filtersArray = [];
let filterObject = {};
gridFilters.forEach((ele, i, arr) => {
filtersArray.push(...arr[i].filters);
});
const _filArr = filtersArray.reduce((acc, {field, operator, value}) => {
filterObject['columnName'] = field;
filterObject['filterValue1'] = value;
filterObject['filterOperator1'] = operator;
filterObject['filterValue2'] = '';
filterObject['filterOperator2'] = '';
columnList.push(filterObject);
return acc;
}, {});
return columnList;
Here, filtersArray
will have 2 array elements like'
Array(2)
0:
field: "objectType"
operator: "contains"
value: "lic"
[[Prototype]]: Object
1:
field: "attributeDisplayName"
operator: "contains"
value: "date"
[[Prototype]]: Object
length: 2
[[Prototype]]: Array(0)
But after pushing the values to the columnList
array, I am seeing the second object (which has attributeDisplayName
) is coming for both values. Basically the first object (which has objectType
) is getting overwritten with second object in array.
This is how the columnList
is getting overwritten
Array(2)
0:
columnName: "attributeDisplayName"
filterOperator1: "contains"
filterOperator2: ""
filterValue1: "date"
filterValue2: ""
[[Prototype]]: Object
1:
columnName: "attributeDisplayName"
filterOperator1: "contains"
filterOperator2: ""
filterValue1: "date"
filterValue2: ""
[[Prototype]]: Object
length: 2
[[Prototype]]: Array(0)
Can some one please suggest what I am doing wrong here. Thanks
[<>]
snippet editor. – mplungjanfilterObject
is always the same object. You keep modifying it in the loop. Just create a new one each loop. – VLAZreduce
anything, usemap
orforEach
will do – ProGu_filArr
, you're misusingreduce
to replace a simpleforEach
. – connexo