skip to Main Content

This is a follow on from a previous question I had that I got an answer for.
Original Question with Answer: Replicate an SQL query in javascript or jquery

This time I’m trying to find a specific set of words in the comment, I can pull out the records but it’s not pulling the records with more words than it’s looking for.

Array:

[
{
    "transaction_id": "44b36e13-ba02-42a6-8b3a-82f6096ec3ba",
    "timestamp": "2021-03-03 18:28:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Winston Bolt",
    "account_id": "61408386"
},
{
    "transaction_id": "6482c419-ff8c-4121-98ae-6f2437646ba8",
    "timestamp": "2021-03-03 17:46:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Winston Bolt",
    "account_id": "61453979"
},
{
    "transaction_id": "0ab6c0cd-4389-4848-92f2-60b54982d5c7",
    "timestamp": "2021-03-03 17:42:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Winston Bolt",
    "account_id": "61428024"
},
{
    "transaction_id": "11580df3-6403-4cf9-a8b0-38b8b1b1b25f",
    "timestamp": "2021-03-03 17:28:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Erin Cox",
    "account_id": "61342083"
},
{
    "transaction_id": "3c6f93e5-ce24-405b-9558-44b2bf3f3653",
    "timestamp": "2021-03-03 17:26:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Ai Musori",
    "account_id": "61712295"
},
{
    "transaction_id": "a14712a0-8808-4686-99b2-ed9639d61fbc",
    "timestamp": "2021-03-03 15:15:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Ai Musori",
    "account_id": "61977423"
},
{
    "transaction_id": "def6ac07-6a7a-48c4-8534-0c68e8ddc46b",
    "timestamp": "2021-03-03 09:06:00",
    "direction": "incoming",
    "type": "transfer",
    "change": "999",
    "comment": "Business Payment: Storefront Restock x1100",
    "requestor": "Sean Tinker",
    "account_id": "61747726"
},
{
    "transaction_id": "f57432d0-b744-41ef-b6b8-af77b81b17ec",
    "timestamp": "2021-03-03 08:59:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: renewal",
    "requestor": "Sarah Ableton",
    "account_id": "61171658"
},
{
    "transaction_id": "ddcd5fd0-78c2-4e80-a603-09acb589122d",
    "timestamp": "2021-03-03 07:22:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Sean Tinker",
    "account_id": "61111782"
},
{
    "transaction_id": "7e359364-666d-41b3-a2cd-86ffac82e0a1",
    "timestamp": "2021-03-03 03:53:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership Renewal",
    "requestor": "Donovan King",
    "account_id": "61252737"
},
{
    "transaction_id": "448fefc0-4c35-48e4-b9ee-e2ab4e31b8aa",
    "timestamp": "2021-03-03 02:30:00",
    "direction": "incoming",
    "type": "transfer",
    "change": "999",
    "comment": "Business Payment: Storefront Restock",
    "requestor": "Donovan King",
    "account_id": "61994863"
},
{
    "transaction_id": "a4417a9c-fbd6-4b2d-8294-244e35dd7774",
    "timestamp": "2021-03-03 02:27:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Renewal",
    "requestor": "Donovan King",
    "account_id": "61773626"
},

{
    "transaction_id": "4b294212-9451-4bc8-8950-3810c7e59765",
    "timestamp": "2021-03-06 12:24:00",
    "direction": "incoming",
    "type": "purchase",
    "change": "999",
    "comment": "Business Services Payment: Membership",
    "requestor": "Gomer Colton",
    "account_id": "61267568"
},
{
    "transaction_id": "e827c214-cab2-48e7-8367-8061ed633988",
    "timestamp": "2021-03-04 01:41:00",
    "direction": "outgoing",
    "type": "transfer",
    "change": "20000",
    "comment": "Cassie car down payment",
    "requestor": "Dean Watson",
    "account_id": "61209808"
},
{
    "transaction_id": "47c64f4b-3196-477d-a336-be4fca4903b1",
    "timestamp": "2021-03-04 01:41:00",
    "direction": "outgoing",
    "type": "transfer",
    "change": "20",
    "comment": "Cassie car down payment",
    "requestor": "Dean Watson",
    "account_id": "61209808"
},
{
    "transaction_id": "b8833152-c541-40f9-b7cc-cfb6dc51cd02",
    "timestamp": "2021-03-03 15:24:00",
    "direction": "outgoing",
    "type": "transfer",
    "change": "100",
    "comment": "Employee Payment: Expemses",
    "requestor": "Dean Watson",
    "account_id": "61762557"
},
{
    "transaction_id": "3bf5298b-7411-4180-adeb-72abfa2bb903",
    "timestamp": "2021-03-03 00:30:00",
    "direction": "incoming",
    "type": "withdraw",
    "change": "10000",
    "comment": "",
    "requestor": "Otto Delmar",
    "account_id": "64397152"
},
{
    "transaction_id": "cd394900-23c5-4130-86ca-a55671b3963c",
    "timestamp": "2021-03-02 20:26:00",
    "direction": "incoming",
    "type": "transfer",
    "change": "999",
    "comment": "Business Payment: Storefront Restock x4500 @ 16",
    "requestor": "Marlo Stanfield",
    "account_id": "61752944"
},
{
    "transaction_id": "d99042fa-ca26-4326-aa5d-5a8aafa1fce0",
    "timestamp": "2021-03-02 20:21:00",
    "direction": "outgoing",
    "type": "deposit",
    "change": "20000",
    "comment": "Business Payment: Bullshit fine coverage",
    "requestor": "Dean Watson",
    "account_id": "61762557"
}
]

Example:
In this case the Array is in the data

var restock_string = 'Business Payment: Storefront Restock';

// Filter out transfers only
const transfer_filter = data.filter(o => o['type'] === 'transfer')

// Filter out transfers for restock reimbursments
const restock_filter = transfer_filter.filter(o =>
   o['comment'].toLowerCase() === restock_string.toLowerCase())

This case works fine as long as there is no other characters in the comment. Unfortunately people can’t follow direction and add quotes or extra info to the line and it gets missed when parsed. I made a regex that just looks for the words "Storefront Restock" but I can’t figure out how to use it with the filter.

Regex:
/(Storefront) (Restock)/gi

2

Answers


  1. Use RegExp#test:

    transfer_filter.filter(o => /Storefront Restock/i.test(o.comment))
    
    Login or Signup to reply.
  2. Something like that ?

    let rx= new RegExp('(?=.*Storefront)(?=.*Restock)','i' )
        
    let filterElms = data.filter( ({type,comment})=> type==='transfer' && rx.test(comment))
    
    const data = 
      [ { transaction_id : '44b36e13-ba02-42a6-8b3a-82f6096ec3ba'
        , timestamp      : '2021-03-03 18:28:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Winston Bolt'
        , account_id     : '61408386'
        } 
      , { transaction_id : '6482c419-ff8c-4121-98ae-6f2437646ba8'
        , timestamp      : '2021-03-03 17:46:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Winston Bolt'
        , account_id     : '61453979'
        } 
      , { transaction_id : '0ab6c0cd-4389-4848-92f2-60b54982d5c7'
        , timestamp      : '2021-03-03 17:42:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Winston Bolt'
        , account_id     : '61428024'
        } 
      , { transaction_id : '11580df3-6403-4cf9-a8b0-38b8b1b1b25f'
        , timestamp      : '2021-03-03 17:28:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Erin Cox'
        , account_id     : '61342083'
        } 
      , { transaction_id : '3c6f93e5-ce24-405b-9558-44b2bf3f3653'
        , timestamp      : '2021-03-03 17:26:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Ai Musori'
        , account_id     : '61712295'
        } 
      , { transaction_id : 'a14712a0-8808-4686-99b2-ed9639d61fbc'
        , timestamp      : '2021-03-03 15:15:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Ai Musori'
        , account_id     : '61977423'
        } 
      , { transaction_id : 'def6ac07-6a7a-48c4-8534-0c68e8ddc46b'
        , timestamp      : '2021-03-03 09:06:00'
        , direction      : 'incoming'
        , type           : 'transfer'
        , change         : '999'
        , comment        : 'Business Payment: Storefront Restock x1100'
        , requestor      : 'Sean Tinker'
        , account_id     : '61747726'
        } 
      , { transaction_id : 'f57432d0-b744-41ef-b6b8-af77b81b17ec'
        , timestamp      : '2021-03-03 08:59:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: renewal'
        , requestor      : 'Sarah Ableton'
        , account_id     : '61171658'
        } 
      , { transaction_id : 'ddcd5fd0-78c2-4e80-a603-09acb589122d'
        , timestamp      : '2021-03-03 07:22:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Sean Tinker'
        , account_id     : '61111782'
        } 
      , { transaction_id : '7e359364-666d-41b3-a2cd-86ffac82e0a1'
        , timestamp      : '2021-03-03 03:53:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership Renewal'
        , requestor      : 'Donovan King'
        , account_id     : '61252737'
        } 
      , { transaction_id : '448fefc0-4c35-48e4-b9ee-e2ab4e31b8aa'
        , timestamp      : '2021-03-03 02:30:00'
        , direction      : 'incoming'
        , type           : 'transfer'
        , change         : '999'
        , comment        : 'Business Payment: Storefront Restock'
        , requestor      : 'Donovan King'
        , account_id     : '61994863'
        } 
      , { transaction_id : 'a4417a9c-fbd6-4b2d-8294-244e35dd7774'
        , timestamp      : '2021-03-03 02:27:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Renewal'
        , requestor      : 'Donovan King'
        , account_id     : '61773626'
        } 
      , { transaction_id : '4b294212-9451-4bc8-8950-3810c7e59765'
        , timestamp      : '2021-03-06 12:24:00'
        , direction      : 'incoming'
        , type           : 'purchase'
        , change         : '999'
        , comment        : 'Business Services Payment: Membership'
        , requestor      : 'Gomer Colton'
        , account_id     : '61267568'
        } 
      , { transaction_id : 'e827c214-cab2-48e7-8367-8061ed633988'
        , timestamp      : '2021-03-04 01:41:00'
        , direction      : 'outgoing'
        , type           : 'transfer'
        , change         : '20000'
        , comment        : 'Cassie car down payment'
        , requestor      : 'Dean Watson'
        , account_id     : '61209808'
        } 
      , { transaction_id : '47c64f4b-3196-477d-a336-be4fca4903b1'
        , timestamp      : '2021-03-04 01:41:00'
        , direction      : 'outgoing'
        , type           : 'transfer'
        , change         : '20'
        , comment        : 'Cassie car down payment'
        , requestor      : 'Dean Watson'
        , account_id     : '61209808'
        } 
      , { transaction_id : 'b8833152-c541-40f9-b7cc-cfb6dc51cd02'
        , timestamp      : '2021-03-03 15:24:00'
        , direction      : 'outgoing'
        , type           : 'transfer'
        , change         : '100'
        , comment        : 'Employee Payment: Expemses'
        , requestor      : 'Dean Watson'
        , account_id     : '61762557'
        } 
      , { transaction_id : '3bf5298b-7411-4180-adeb-72abfa2bb903'
        , timestamp      : '2021-03-03 00:30:00'
        , direction      : 'incoming'
        , type           : 'withdraw'
        , change         : '10000'
        , comment        : ''
        , requestor      : 'Otto Delmar'
        , account_id     : '64397152'
        } 
      , { transaction_id : 'cd394900-23c5-4130-86ca-a55671b3963c'
        , timestamp      : '2021-03-02 20:26:00'
        , direction      : 'incoming'
        , type           : 'transfer'
        , change         : '999'
        , comment        : 'Business Payment: Storefront Restock x4500 @ 16'
        , requestor      : 'Marlo Stanfield'
        , account_id     : '61752944'
        } 
      , { transaction_id : 'd99042fa-ca26-4326-aa5d-5a8aafa1fce0'
        , timestamp      : '2021-03-02 20:21:00'
        , direction      : 'outgoing'
        , type           : 'deposit'
        , change         : '20000'
        , comment        : 'Business Payment: Bullshit fine coverage'
        , requestor      : 'Dean Watson'
        , account_id     : '61762557'
        } 
      ] 
    
    let rx= new RegExp('(?=.*Storefront)(?=.*Restock)','i' )
        
    let filterElms = data.filter( ({type,comment})=> type==='transfer' && rx.test(comment))
    
    console.log( filterElms )
    .as-console-wrapper {max-height: 100% !important;top: 0;}
    .as-console-row::after {display: none !important;}
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search