移除空对象

huangapple go评论94阅读模式
英文:

remove of null object

问题

我不会返回翻译好的内容。

英文:

how can I not release data if two attributes are empty?

  1. const fork = [
  2. { from: 'client', msg: null, for: null },
  3. { from: 'client', msg: '2222222222222', for: null },
  4. { from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }
  5. ];
  6. 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

  1. const fork = [
  2. { from: 'client', msg: null, for: null }, // remove line full
  3. { from: 'client', msg: '2222222222222', for: null }, // remove for
  4. { from: 'server', msg: null, for: 'data/64.....' } // remove msg
  5. ];
  6. console.log(message)

答案1

得分: 2

  1. const fork = [{ from: 'client', msg: null, for: null }, { from: 'client', msg: '2222222222222', for: null }, { from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }];
  2. const result = fork
  3. .filter(o => ['msg', 'for'].some(k => o[k] !== null))
  4. .map(o => Object.fromEntries(Object
  5. .entries(o)
  6. .filter(([, v]) => v !== null)
  7. ));
  8. 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)
));

  1. console.log(result);

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

答案2

得分: 0

以下是代码的翻译部分:

  1. const fork = [
  2. { from: 'client', msg: null, for: null },
  3. { from: 'client', msg: '2222222222222', for: null },
  4. { from: 'server', msg: 'wqqqqqqqqqqqq', for: 'data/64.....' }
  5. ];
  6. const result = fork.map(item => {
  7. if (Object.values(item).filter(i => i === null).length > 1) return null;
  8. const obj = { ...item };
  9. for (const key in obj) {
  10. if (obj[key] === null) {
  11. delete obj[key];
  12. }
  13. }
  14. return obj;
  15. }).filter(item => item !== null);
  16. console.log(result);

请注意,这只是代码的翻译,没有其他内容。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const fork = [
  2. { from: &#39;client&#39;, msg: null, for: null },
  3. { from: &#39;client&#39;, msg: &#39;2222222222222&#39;, for: null },
  4. { from: &#39;server&#39;, msg: &#39;wqqqqqqqqqqqq&#39;, for: &#39;data/64.....&#39; }
  5. ];
  6. const result = fork.map(item =&gt; {
  7. if (Object.values(item).filter(i =&gt; i === null).length &gt; 1) return null
  8. const obj = {...item}
  9. for (const key in obj) {
  10. if (obj[key] === null) {
  11. delete obj[key]
  12. }
  13. }
  14. return obj
  15. }).filter(item =&gt; item !== null)
  16. console.log(result)

<!-- end snippet -->

答案3

得分: 0

使用 reduce 方法

  1. const fork = [
  2. { from: "client", msg: null, for: null },
  3. { from: "client", msg: "2222222222222", for: null },
  4. { from: "server", msg: "wqqqqqqqqqqqq", for: "data/64....." },
  5. ];
  6. const res = fork.reduce((acc, curr) => {
  7. const obj = { from: curr.from };
  8. ["msg", "for"].forEach((key) => (curr[key] !== null && (obj[key] = curr[key])));
  9. return Object.keys(obj).length > 1 ? acc.concat(obj) : acc;
  10. }, []);
  11. console.log(res);
英文:

Using reduce method

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const fork = [
  2. { from: &quot;client&quot;, msg: null, for: null },
  3. { from: &quot;client&quot;, msg: &quot;2222222222222&quot;, for: null },
  4. { from: &quot;server&quot;, msg: &quot;wqqqqqqqqqqqq&quot;, for: &quot;data/64.....&quot; },
  5. ];
  6. const res = fork.reduce((acc, curr) =&gt; {
  7. const obj = { from: curr.from };
  8. [&quot;msg&quot;, &quot;for&quot;].forEach((key) =&gt; (curr[key] !== null &amp;&amp; (obj[key] = curr[key])));
  9. return Object.keys(obj).length &gt; 1 ? acc.concat(obj) : acc;
  10. }, []);
  11. console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 05:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定