1

Below is my array of objects

[
{ field: 'EXPECTEDDATE', operator: 'gte', value: '20200601' },
{ field: 'EXPECTEDDATE', operator: 'lte', value: '20200630' },
{ field: 'PYNAME', operator: 'contains', value: 'abc' },
{ field: 'DEPT', operator: 'eq', value: 'IT' }
]

I want to change the format of it to pass it to an API like below

{
attibuteName: 'PYNAME',
filterOperator1: 'contains',
filterValue1: 'abc',
filterOperator2: '',
filterValue2: '',
},
{
attibuteName: 'DEPT',
filterOperator1: 'eq',
filterValue1: 'IT',
filterOperator2: '',
filterValue2: '',
},
 {
attibuteName: 'EXPECTEDDATE',
filterOperator1: 'gte',
filterValue1: '20200601',
filterOperator2: 'lte',
filterValue2: '20200630',
}

Below is my current code and I am stuck at merging the filterOperator2 and filterValue2 to same object for the same field in forEach loop.

filtersArray.forEach((element, index, array) => {
   filterObject = gridColumns.find(currField => currField.attributeName === element.field);
   const filop1 = 'filterOperator1';
   const filval1 = 'filterValue1';
   const filop2 = 'filterOperator2';
   const filval2 = 'filterValue2';
   filterObject[filop1] = element.operator;      
   filterObject[filval1] = element.value;
   columnList.push(filterObject);
});
return columnList;
}

Please suggest how can I do this. Thanks.

1
  • is destructuring possible here in this case? Commented Jun 29, 2020 at 6:49

2 Answers 2

1

You can do it with .reduce(). This solution is pretty hardcoded and not very dynamic

let arr = [
{ field: 'EXPECTEDDATE', operator: 'gte', value: '20200601' },
{ field: 'EXPECTEDDATE', operator: 'lte', value: '20200630' },
{ field: 'PYNAME', operator: 'contains', value: 'abc' },
{ field: 'DEPT', operator: 'eq', value: 'IT' }
]

let result = arr.reduce((a,v) => {
   let index = a.findIndex(el => el.attibuteName === v.field);
   if(index !== -1){
      a[index].filterOperator2 = v.operator;
      a[index].filterValue2 = v.value;
      return a;
   }
   a.push({
      attibuteName: v.field,
      filterOperator1: v.operator,
      filterValue1: v.value,
      filterOperator2: "",
      filterValue2: ""
   })
   return a;
},[])

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

I think you can do this:

var arr= [
{ field: 'EXPECTEDDATE', operator: 'gte', value: '20200601' },
{ field: 'EXPECTEDDATE', operator: 'lte', value: '20200630' },
{ field: 'PYNAME', operator: 'contains', value: 'abc' },
{ field: 'DEPT', operator: 'eq', value: 'IT' }
]

var res = arr.reduce((acc, {field, operator, value})=>{
  if(acc[field]){
    if(!acc[field].filterValue2){
      acc[field].filterValue2= value
    }
    if(!acc[field].filterOperator2){
      acc[field].filterOperator2= operator
    }
 }else{
  acc[field] =  {
    attibuteName: field,
    filterOperator1:operator,
    filterValue1: value,
    filterOperator2: '',
    filterValue2: '',
  };

 }
return acc
}, {})

console.log(Object.values(res))

3 Comments

But using this approach, how can I get the remaining properties coming from 'filterObject'?
I'm just destructuring all the properties and using them. You can destructure any other property just like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.