Reputation: 524
I have an array of objects converted from a CSV file and there are a lot of junk values (or values i dont need in the rest of my program).
{
'Mailbox ID': '411042035',
'First Name': 'Amy',
'Last Name': 'Doe',
Extension: '117444',
Email: '[email protected]',
'Mobile Phone': '',
Department: 'Department',
'Job Title': '',
Roles: 'Standard (International)',
'User Groups': '',
Site: '',
'Cost Center Code': '',
'Direct Number (DID)': '(555) 555-7804',
'Phone Make/Model': '12x f56',
'Ship to: Street Address - Line 1': '555 Main St',
'Ship to: Street Address - Line 2': '',
'Ship to: City': 'Philadelphia',
'Ship to: State/Province/County': 'PA',
'Ship to: Postal Code': '19000',
'Ship to: Country': 'US',
'Emergency Address': '555 Main St, 5TH FL, PHILADELPHIA, PA, 19000, US',
'Emergency Response Location': '',
'Fax Cover Page Address': '',
'User Language': 'English (U.S.)',
'Time Zone': '(GMT-05:00) Eastern Time (US & Canada)',
Status: 'Enabled',
'Assigned Call Queues': ''
}
I want to filer an array of objects like this where
I want to use Lodash to add conditions like this, but im not sure of the syntax or the possibilities. How would i also add conditions to the statement?
this is what I have and it is not working:
let records = await _.filter(RC_file_contents, (line) => {
let _line = line;
let data = [];
if(!_line['First Name'].includes('VE.') && !_line['Department'] == 'Physician' && !_line['Email'].includes('.comx')){
data.push(line)
}
return data
})
Upvotes: 0
Views: 101
Reputation: 1912
There's a lot going on here. It looks like your combining different methods. The filter method determines whether to keep the item or not by the return value of the callback. If the return value is truthy the item is kept, if it's falsey then it's removed. The main reason it isn't working it you are returning an array data
on every item. An array is a truthy
value, therefore no items are being removed.
You can do this without lodash since arrays have .filter already.
_.filter isn't async, so you don't need the await
let records = RC_file_contents.filter(_line => {
// Return true or false to keep or remove the item
return (
!_line['First Name'].includes('VE.')
&& _line.Department !== 'Physician'
&& !/^73/.test(_line.Extension) // Use regex to test what extension starts with (^)
&& !/\.comx$/.test(_line.Email) // Use regex to test what email ends with ($)
&& !/^\d{3,}/.test(_line.Email) // Email doens't start with 3 or more numbers
);
})
Upvotes: 1