我試圖過濾一個陣列,但我沒有得到預期的解決方案。有人可以幫忙嗎。在這個陣列中,如果 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": "[email protected]",
"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": "[email protected]",
"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"
}
]
}
]
我嘗試了下面的代碼(請參閱小提琴鏈接)但它沒有按預期作業。如果它包含 to.type == 'CUSTOMER',它將消除陣列條目。我只想消除陣列中的最后兩個條目。提前致謝
https://jsfiddle.net/4m9kgfpv/
uj5u.com熱心網友回復:
使用Array#filter, 迭代陣列并在以下情況下保留元素:
from.organization_details.type是CUSTOMER
或者
to有一個元素,其中type是CUSTOMER(使用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": "[email protected]",
"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": "[email protected]",
"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);
uj5u.com熱心網友回復:
有一個名為 filter 的函式用于陣列,它完全符合你的要求,你可以在這里閱讀它,但我也在這里寫了一個例子。
假設我們有陣列并且我們想要過濾所有小于或等于 2 的值,我們應該在 array.filter 回呼中創建這樣一個條件,只有大于 2 的值才回傳 true。在您的情況下,您只需要檢查屬性以匹配您想要的內容,希望有所幫助。
let array = [1, 2, 3, 4, 5];
console.log(array.filter(element => {
return element > 2;
}));
uj5u.com熱心網友回復:
這是您的解決方案。如果您的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: "[email protected]",
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: "[email protected]",
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404631.html
標籤:
