how can I not release data if two attributes are empty?
const fork = [
{ from: 'client', msg: null, for: null },
{ from: 'client', msg: '2222222222222', for: null },
{ from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }
];
console.log(message)
These are three sample entries, in fact they are always replenished. And there are more.
I need to do this, if two attributes are empty then do not send if somewhere null then do not release this value
const fork = [
{ from: 'client', msg: null, for: null }, // remove line full
{ from: 'client', msg: '2222222222222', for: null }, // remove for
{ from: 'server', msg: null, for: 'data/64.....' } // remove msg
];
console.log(message)
CodePudding user response:
You could filter the array and map the rest.
const
fork = [{ from: 'client', msg: null, for: null }, { from: 'client', msg: '2222222222222', for: null }, { from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }];
result = fork
.filter(o => ['msg', 'for'].some(k => o[k] !== null))
.map(o => Object.fromEntries(Object
.entries(o)
.filter(([, v]) => v !== null)
));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
const fork = [
{ from: 'client', msg: null, for: null },
{ from: 'client', msg: '2222222222222', for: null },
{ from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }
];
const result = fork.map(item => {
if (Object.values(item).filter(i => i === null).length > 1) return null
const obj = {...item}
for (const key in obj) {
if (obj[key] === null) {
delete obj[key]
}
}
return obj
}).filter(item => item !== null)
console.log(result)