英文:
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#filter
与 Array#every
或 Array#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:"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));
// or !values.includes(c.status.name) if no complex matching logic is required
console.log(newComments);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论