I am trying to filter non-array JSON object as the following snippet
const filter = { filterRows: ['one'] };
const templateMapper = {
'one': {
title: 'one',
},
'two': {
title: 'two',
},
'three': {
title: 'three',
},
}
const filterDialogView = filter.filterRows;
const filterTemplateMapper = [templateMapper].filter(row => !filterDialogView.includes(row));
console.log(filterTemplateMapper);
But it's not filtering
I am getting following output
[
{
"one": {
"title": "one"
},
"two": {
"title": "two"
},
"three": {
"title": "three"
}
}
]
Desire output
{
"two": {
"title": "two"
},
"three": {
"title": "three"
}
}
I want to filter row based on filterRows for example if filterRows contain one as above JSON then one should be removed from the templateMapper
You can use Object.fromEntries to build object back from filtered entries
Here idea is:-
const filter = { filterRows: ['one'] };
const template = {'one': {title: 'one',},'two': {title: 'two',},'three': {title: 'three',}}
const filterDialogView = filter.filterRows;
const final = Object.entries(template).filter(([row])=> !filterDialogView.includes(row))
console.log(Object.fromEntries(final));
If you environment doesn't support Object.fromEntries you can use this
const filter = { filterRows: ['one'] };
const template = {'one': {title: 'one',},'two': {title: 'two',},'three': {title: 'three',}}
const filterDialogView = filter.filterRows;
const final = Object.entries(template).filter(([row])=> !filterDialogView.includes(row))
const output = final.reduce((op,[key,value])=>{
op[key] = value
return op
},{})
console.log(output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With