Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

array manipulation – How to remove a specific entry from array based on a condition

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/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using Array#filter, iterate over the array and keep the element if:

from.organization_details.type is CUSTOMER

OR

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading