移除空对象

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

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: &#39;client&#39;, msg: null, for: null },
  { from: &#39;client&#39;, msg: &#39;2222222222222&#39;, for: null },
  { from: &#39;server&#39;, msg: &#39;wqqqqqqqqqqqq&#39;, for: &#39;data/64.....&#39; }
];

const result = fork.map(item =&gt; {
  if (Object.values(item).filter(i =&gt; i === null).length &gt; 1) return null
  const obj = {...item}
  for (const key in obj) {
    if (obj[key] === null) {
      delete obj[key]
    }
  }
  return obj
}).filter(item =&gt; 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: &quot;client&quot;, msg: null, for: null },
  { from: &quot;client&quot;, msg: &quot;2222222222222&quot;, for: null },
  { from: &quot;server&quot;, msg: &quot;wqqqqqqqqqqqq&quot;, for: &quot;data/64.....&quot; },
];

const res = fork.reduce((acc, curr) =&gt; {
  const obj = { from: curr.from };
  [&quot;msg&quot;, &quot;for&quot;].forEach((key) =&gt; (curr[key] !== null &amp;&amp; (obj[key] = curr[key])));
  return Object.keys(obj).length &gt; 1 ? acc.concat(obj) : acc;
}, []);

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:

确定