-1

I have an array of objects such as:

{
    "value": [
        {
            "attachment_text": "Support: hello there! I need some help syncing records",
            "ticket_text": "Ticket 1537 from ronburgundy@news.com"
        },
        {
            "attachment_text": "Copied from original request - 401 unathorized",
            "ticket_text": "Ticket 1459 from klaus.kinsky@kinsky.com"
        },
        {
            "attachment_text": "Contact request form received",
            "ticket_text": "Ticket 1173 from sophia@copolla.com"
        },
        {
            "attachment_text": "Hello, we need to troubleshoot some problems",
            "ticket_text": "Ticket 1591 from pat@pending.com"
        },
        {
            "attachment_text": "Contact request form received",
            "ticket_text": "Ticket 1483 from bugs@bunny.com"
        },
        {
            "attachment_text": "Contact request form received",
            "ticket_text": "Ticket 1393 from elmer@fudd.com"
        }
    ]
}

What javascript can I use to remove any objects where the attachment_text field contains the string "Contact request"?

Preferably pure javascript (i.e. not a library such as lodash)

KoreMike
  • 91
  • 7

3 Answers3

7

You can use the filter method of Array Objects in JavaScript. I have created a demo for you below:

var data = {
  "value": [{
      "attachment_text": "Support: hello there! I need some help syncing records",
      "ticket_text": "Ticket 1537 from ronburgundy@news.com"
    },
    {
      "attachment_text": "Copied from original request - 401 unathorized",
      "ticket_text": "Ticket 1459 from klaus.kinsky@kinsky.com"
    },
    {
      "attachment_text": "Contact request form received",
      "ticket_text": "Ticket 1173 from sophia@copolla.com"
    },
    {
      "attachment_text": "Hello, we need to troubleshoot some problems",
      "ticket_text": "Ticket 1591 from pat@pending.com"
    },
    {
      "attachment_text": "Contact request form received",
      "ticket_text": "Ticket 1483 from bugs@bunny.com"
    },
    {
      "attachment_text": "Contact request form received",
      "ticket_text": "Ticket 1393 from elmer@fudd.com"
    }
  ]
}

const result = data.value.filter((data) => !data["attachment_text"].includes("Contact request"))
console.log(result)
Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28
1

You can use the filter method, which will keep each item where a specific condition results to true.
In your case, the condition can be a simple includes method to check for the presence of the "Contact request" string.

var d = {
  "value": [
    {
      "attachment_text": "Support: hello there! I need some help syncing records",
      "ticket_text": "Ticket 1537 from ronburgundy@news.com"
    },
    {
      "attachment_text": "Copied from original request - 401 unathorized",
      "ticket_text": "Ticket 1459 from klaus.kinsky@kinsky.com"
    },
    {
      "attachment_text": "Contact request form received",
      "ticket_text": "Ticket 1173 from sophia@copolla.com"
    },
    {
      "attachment_text": "Hello, we need to troubleshoot some problems",
      "ticket_text": "Ticket 1591 from pat@pending.com"
    },
    {
      "attachment_text": "Contact request form received",
      "ticket_text": "Ticket 1483 from bugs@bunny.com"
    },
    {
      "attachment_text": "Contact request form received",
      "ticket_text": "Ticket 1393 from elmer@fudd.com"
    }
  ]
}

// result contains what you desires
var result = d.value.filter((d) => !d["attachment_text"].includes("Contact request"))
Claire
  • 330
  • 2
  • 5
0

Use

value.filter(x=>x.attachment_text.indexOf('Contact request') > -1);

The indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

and works fine perfectly with every browser.

Hrishikesh Kale
  • 6,140
  • 4
  • 18
  • 27