let currentFilter: Product = {
name: 'iphone',
price: 30,
createdDate: '11-11-2020'
}
My code is structured around a specific filter.
The filter data is structured in the following format:
I have a table with different filters. Each filter event exposes data in a specific format:
For example, if I select the name filter, the data will look like this:
"filters":{"name ":{"value":"apple","matchMode":"equals"}}
If I choose the price filter, it will look like this:
"filters":{"price ":{"value":"30","matchMode":"equals"}}
I then destructure the data based on its properties.
let {name, price, createdDate} = event.filters;
console.log(name, 'name'); // I will receive the value for "name" as {"value":"30","matchMode":"equals"}
console.log(price, 'price'); // It will be undefined
After obtaining the filtered name value, I need to map it to the corresponding Product Interface property.
I want to map the selected values and leave the remaining properties empty. How can I achieve this using object destructuring in ES6?
I can use a switch case statement to achieve this by checking the name and mapping the value. Is there a more efficient way to do this using object destructuring?
Here is a solution I have come up with:
Receiving dynamic objects:
let dynamicFilter = event.filters; // "filters":{"name ":{"value":"apple","matchMode":"equals"}}
// Initial predefined values:
let currentFilter: Product = {
name: 'iphone',
price: 30,
createdDate: '11-11-2020'
}
// Mapping with predefined objects:
for (let property in currentFilter) {
for(let m in dynamicFilter){
if(m === property)
{
console.log(`1Object key= ${property} and 2ObjectValues= ${dynamicFilter[m].value}` );
// Assigning incoming values to matched object property
currentFilter[property] = dynamicFilter[m].value;
}
}
};