数组操作 - 如何根据条件从数组中删除特定条目
array manipulation - How to remove a specific entry from array based on a condition
我正在尝试过滤一个数组,但没有得到预期的解决方案。
有人可以帮忙吗?在这个数组中,如果 from.organization_details.type 是 'SELLER_USER' 并且 to.type 值是 'OPERATOR',我需要过滤掉。
此外,如果 from.organization_details.type == 'OPERATOR_USER' 并且 to[0].type 值为 'SHOP'
[
{
"body": "Could you please elaborate on the discount",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
},
{
"body": "for testing purpose",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "i need to know about warranty ",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
},
{
"body": "this is a test messgae from seller on dec 17 21",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": {
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
"type": "SHOP_USER"
},
"to": [
{
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
}
]
},
{
"body": "hi there",
"from": {
"display_name": "Operator",
"organization_details": {
"display_name": "Operator",
"type": "OPERATOR"
},
"type": "OPERATOR_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
}
]
},
{
"body": "hello this is a test messsage on jan 5 2022",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "this is a message sent to operator ",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": {
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
"type": "SHOP_USER"
},
"to": [
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "this is a msg from operator to seller ",
"from": {
"display_name": "Operator",
"organization_details": {
"display_name": "Operator",
"type": "OPERATOR"
},
"type": "OPERATOR_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
}
]
我尝试了下面的代码(请参阅 fiddle link)但它没有按预期工作。如果它包含 to.type == 'CUSTOMER',它会删除数组条目。我只想消除数组中的最后两个条目。
提前致谢
有一个名为 filter for arrays 的函数可以完全满足您的需求,您可以阅读它 here,但我也在这里写了一个示例。
假设我们有一个数组,并且我们想要过滤所有小于或等于 2 的值,我们应该在 array.filter 回调中创建这样一个条件,该条件仅值大于 2 return真的。在您的情况下,您只需要检查属性以匹配您想要的内容,希望对您有所帮助。
let array = [1, 2, 3, 4, 5];
console.log(array.filter(element => {
return element > 2;
}));
这是您的解决方案。如果您的 to 数组
中只有 1 个元素,它将起作用
const arr = [
{
body: "Could you please elaborate on the discount",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "SELLER_USER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "OPERATOR",
},
],
},
{
body: "for testing purpose",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "i need to know about warranty ",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
],
},
{
body: "this is a test messgae from seller on dec 17 21",
from: {
display_name: "taufiqpainter@gmail.com",
organization_details: {
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
type: "SHOP_USER",
},
to: [
{
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
],
},
{
body: "hi there",
from: {
display_name: "Operator",
organization_details: {
display_name: "Operator",
type: "OPERATOR",
},
type: "OPERATOR_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
],
},
{
body: "hello this is a test messsage on jan 5 2022",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "this is a message sent to operator ",
from: {
display_name: "taufiqpainter@gmail.com",
organization_details: {
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
type: "SHOP_USER",
},
to: [
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "this is a msg from operator to seller ",
from: {
display_name: "Operator",
organization_details: {
display_name: "Operator",
type: "OPERATOR",
},
type: "OPERATOR_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
],
},
];
// Previous array length
console.log(arr.length);
arr.forEach((value, i) => {
// If the condition is true than remove that value from the array
if (
(value.from.organization_details.type == "SELLER_USER" &&
value.to[0].type === "OPERATOR") ||
(value.from.organization_details.type == "OPERATOR_USER" && value.to[0].type === "SHOP")
) {
arr.splice(i, 1);
}
});
// Array length after removing data from array
console.log(arr.length);
使用 Array#filter
,迭代数组并保留元素如果:
from.organization_details.type
is CUSTOMER
或
to
has an element where type
is CUSTOMER
(use Array#some
)
const conferenceDays = [
{
"body": "Could you please elaborate on the discount",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
},
{
"body": "for testing purpose",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER"},
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "i need to know about warranty ",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
},
{
"body": "this is a test messgae from seller on dec 17 21",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": { "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
"type": "SHOP_USER"
},
"to": [
{ "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" }
]
},
{
"body": "hi there",
"from": {
"display_name": "Operator",
"organization_details": { "display_name": "Operator", "type": "OPERATOR" },
"type": "OPERATOR_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" }
]
},
{
"body": "hello this is a test messsage on jan 5 2022",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "this is a message sent to operator ",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": { "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
"type": "SHOP_USER"
},
"to": [
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "this is a msg from operator to seller ",
"from": {
"display_name": "Operator",
"organization_details": { "display_name": "Operator", "type": "OPERATOR" },
"type": "OPERATOR_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
}
];
const arr = conferenceDays.filter(({ from = {}, to = [] }) =>
(from.organization_details.type === 'CUSTOMER') ||
(to.some(({ type }) => type === 'CUSTOMER'))
);
console.log(arr);
我正在尝试过滤一个数组,但没有得到预期的解决方案。 有人可以帮忙吗?在这个数组中,如果 from.organization_details.type 是 'SELLER_USER' 并且 to.type 值是 'OPERATOR',我需要过滤掉。 此外,如果 from.organization_details.type == 'OPERATOR_USER' 并且 to[0].type 值为 'SHOP'
[
{
"body": "Could you please elaborate on the discount",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
},
{
"body": "for testing purpose",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "i need to know about warranty ",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
},
{
"body": "this is a test messgae from seller on dec 17 21",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": {
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
"type": "SHOP_USER"
},
"to": [
{
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
}
]
},
{
"body": "hi there",
"from": {
"display_name": "Operator",
"organization_details": {
"display_name": "Operator",
"type": "OPERATOR"
},
"type": "OPERATOR_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
}
]
},
{
"body": "hello this is a test messsage on jan 5 2022",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "this is a message sent to operator ",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": {
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
"type": "SHOP_USER"
},
"to": [
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "this is a msg from operator to seller ",
"from": {
"display_name": "Operator",
"organization_details": {
"display_name": "Operator",
"type": "OPERATOR"
},
"type": "OPERATOR_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
}
]
有一个名为 filter for arrays 的函数可以完全满足您的需求,您可以阅读它 here,但我也在这里写了一个示例。
假设我们有一个数组,并且我们想要过滤所有小于或等于 2 的值,我们应该在 array.filter 回调中创建这样一个条件,该条件仅值大于 2 return真的。在您的情况下,您只需要检查属性以匹配您想要的内容,希望对您有所帮助。
let array = [1, 2, 3, 4, 5];
console.log(array.filter(element => {
return element > 2;
}));
这是您的解决方案。如果您的 to 数组
中只有 1 个元素,它将起作用const arr = [
{
body: "Could you please elaborate on the discount",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "SELLER_USER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "OPERATOR",
},
],
},
{
body: "for testing purpose",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "i need to know about warranty ",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
],
},
{
body: "this is a test messgae from seller on dec 17 21",
from: {
display_name: "taufiqpainter@gmail.com",
organization_details: {
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
type: "SHOP_USER",
},
to: [
{
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
],
},
{
body: "hi there",
from: {
display_name: "Operator",
organization_details: {
display_name: "Operator",
type: "OPERATOR",
},
type: "OPERATOR_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
],
},
{
body: "hello this is a test messsage on jan 5 2022",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "this is a message sent to operator ",
from: {
display_name: "taufiqpainter@gmail.com",
organization_details: {
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
type: "SHOP_USER",
},
to: [
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "this is a msg from operator to seller ",
from: {
display_name: "Operator",
organization_details: {
display_name: "Operator",
type: "OPERATOR",
},
type: "OPERATOR_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
],
},
];
// Previous array length
console.log(arr.length);
arr.forEach((value, i) => {
// If the condition is true than remove that value from the array
if (
(value.from.organization_details.type == "SELLER_USER" &&
value.to[0].type === "OPERATOR") ||
(value.from.organization_details.type == "OPERATOR_USER" && value.to[0].type === "SHOP")
) {
arr.splice(i, 1);
}
});
// Array length after removing data from array
console.log(arr.length);
使用 Array#filter
,迭代数组并保留元素如果:
from.organization_details.type
isCUSTOMER
或
to
has an element wheretype
isCUSTOMER
(useArray#some
)
const conferenceDays = [
{
"body": "Could you please elaborate on the discount",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
},
{
"body": "for testing purpose",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER"},
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "i need to know about warranty ",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
},
{
"body": "this is a test messgae from seller on dec 17 21",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": { "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
"type": "SHOP_USER"
},
"to": [
{ "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" }
]
},
{
"body": "hi there",
"from": {
"display_name": "Operator",
"organization_details": { "display_name": "Operator", "type": "OPERATOR" },
"type": "OPERATOR_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" }
]
},
{
"body": "hello this is a test messsage on jan 5 2022",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "this is a message sent to operator ",
"from": {
"display_name": "taufiqpainter@gmail.com",
"organization_details": { "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
"type": "SHOP_USER"
},
"to": [
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "this is a msg from operator to seller ",
"from": {
"display_name": "Operator",
"organization_details": { "display_name": "Operator", "type": "OPERATOR" },
"type": "OPERATOR_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
}
];
const arr = conferenceDays.filter(({ from = {}, to = [] }) =>
(from.organization_details.type === 'CUSTOMER') ||
(to.some(({ type }) => type === 'CUSTOMER'))
);
console.log(arr);