英文:
remove of null object
问题
我不会返回翻译好的内容。
英文:
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)
答案1
得分: 2
const fork = [{ from: 'client', msg: null, for: null }, { from: 'client', msg: '2222222222222', for: null }, { from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }];
const 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);
英文:
You could filter the array and map the rest.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
答案2
得分: 0
以下是代码的翻译部分:
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);
请注意,这只是代码的翻译,没有其他内容。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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)
<!-- end snippet -->
答案3
得分: 0
使用 reduce
方法
const fork = [
{ from: "client", msg: null, for: null },
{ from: "client", msg: "2222222222222", for: null },
{ from: "server", msg: "wqqqqqqqqqqqq", for: "data/64....." },
];
const res = fork.reduce((acc, curr) => {
const obj = { from: curr.from };
["msg", "for"].forEach((key) => (curr[key] !== null && (obj[key] = curr[key])));
return Object.keys(obj).length > 1 ? acc.concat(obj) : acc;
}, []);
console.log(res);
英文:
Using reduce
method
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const fork = [
{ from: "client", msg: null, for: null },
{ from: "client", msg: "2222222222222", for: null },
{ from: "server", msg: "wqqqqqqqqqqqq", for: "data/64....." },
];
const res = fork.reduce((acc, curr) => {
const obj = { from: curr.from };
["msg", "for"].forEach((key) => (curr[key] !== null && (obj[key] = curr[key])));
return Object.keys(obj).length > 1 ? acc.concat(obj) : acc;
}, []);
console.log(res);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论