Im trying to filter an array but im not arriving at the expected solution.
Can someone please help. In this array i need to filter out if from.organization_details.type is ‘SELLER_USER’ and to.type value is ‘OPERATOR’.
Also if from.organization_details.type == ‘OPERATOR_USER’ and to[0].type value is ‘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"
}
]
}
]
I tried below code (Please see fiddle link) but its not working as expected. It eliminates the array entry if it contains to.type == ‘CUSTOMER’. I want to eliminate only last two entries in my array.
Thanks in advance
https://jsfiddle.net/4m9kgfpv/
>Solution :
Using Array#filter, iterate over the array and keep the element if:
from.organization_details.typeisCUSTOMER
OR
tohas an element wheretypeisCUSTOMER(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);