如何根据多个条件筛选对象中的数组

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

How to filter array in object by few conditions

问题

以下是代码的翻译部分:

我有这样一个具有newComments属性的对象

const newComments = {
  commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  comments: [
    {
      id: "123",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f93",
        name: "In work",
        parentId: null,
      },
    },
    {
      id: "124",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f94",
        name: "Note",
        parentId: null,
      },
    },
    {
      id: "125",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f95",
        name: "Canceled",
        parentId: null,
      },
    },
    {
      id: "126",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f96",
        name: "Done",
        parentId: null,
      },
    },
  ],
  dataType: "Tags",
  idAttributeApprovalName: "12-015-123",
};

还有筛选条件:

const values = ["Note", "Canceled", "Done"];

我的任务是返回与这些键不相等的评论:

comment.status.name !== "Note" | "Canceled" | "Done"

换句话说:

const newComments = {
  commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  comments: [
    {
      id: "123",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f93",
        name: "In work",
        parentId: null,
      },
    },
  ],
  dataType: "Tags",
  idAttributeApprovalName: "12-015-123",
};
英文:

I have such an object with newComments:

const newComments = {
  commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  comments: [
    {
      id: "123",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f93",
        name: "In work",
        parentId: null,
      },
    },
    {
      id: "124",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f94",
        name: "Note",
        parentId: null,
      },
    },
    {
      id: "125",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f95",
        name: "Canceled",
        parentId: null,
      },
    },
    {
      id: "126",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f96",
        name: "Done",
        parentId: null,
      },
    },
  ],
  dataType: "Tags",
  idAttributeApprovalName: "12-015-123",
};

There are also filters:

const values = ["Note", "Canceled", "Done"];

My task is to return only comments that are not equal to keys:

comment.status.name !== "Note" | "Canceled" |"Done"

In other words:

const newComments = {
  commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  comments: [
    {
      id: "123",
      status: {
        entity: null,
        id: "1a913152-7809-ec11-8daa-90600b960f93",
        name: "In work",
        parentId: null,
      },
    },
  ],
  dataType: "Tags",
  idAttributeApprovalName: "12-015-123",
};

答案1

得分: 0

你可以使用 Array#filterArray#everyArray#includes 结合使用。

const newComments = {
  commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  comments: [
    { id: "123", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f93", name: "In work", parentId: null } },
    { id: "124", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f94", name: "Note", parentId: null } },
    { id: "125", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f95", name: "Canceled", parentId: null } },
    { id: "126", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f96", name: "Done", parentId: null } },
  ],
  dataType: "Tags",
  idAttributeApprovalName: "12-015-123"
};

const values = ["Note", "Canceled", "Done"];

newComments.comments = newComments.comments.filter(c => 
  values.every(v => c.status.name !== v));
// 或者使用 !values.includes(c.status.name) 如果不需要复杂的匹配逻辑

console.log(newComments);

请注意,以上代码片段是用JavaScript编写的,只提供代码部分的翻译。

英文:

You can use Array#filter in conjunction with Array#every or Array#includes.

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

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

const newComments={commentDirectoryId:&quot;ee63997c-01d5-ec11-8dad-e116bd673e14&quot;,comments:[{id:&quot;123&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f93&quot;,name:&quot;In work&quot;,parentId:null}},{id:&quot;124&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f94&quot;,name:&quot;Note&quot;,parentId:null}},{id:&quot;125&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f95&quot;,name:&quot;Canceled&quot;,parentId:null}},{id:&quot;126&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f96&quot;,name:&quot;Done&quot;,parentId:null}},],dataType:&quot;Tags&quot;,idAttributeApprovalName:&quot;12-015-123&quot;};
const values = [&quot;Note&quot;, &quot;Canceled&quot;, &quot;Done&quot;];
newComments.comments = newComments.comments.filter(c =&gt; 
                        values.every(v =&gt; c.status.name !== v));
// or !values.includes(c.status.name) if no complex matching logic is required
console.log(newComments);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 01:41:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364764.html
匿名

发表评论

匿名网友

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

确定