Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to filter an object from an array of objects. Below is the array of the object.
const groupsList = [
    {
    "groupId": 1234,
    "adminId": 52,
    "name": "HHLC Health co",
    "directorName": "Mr Daric",
    "fax": "5556565655",
    "email": "test@test.com",
    "status": 1,
    "address1": "Akshya Nagar 1st Block 1st Cross",
    "districtName": "Berkshire",
    "addedDate": "2020-02-01T04:44:54.000Z",
    "lastModified": "2020-02-25T10:30:47.000Z"
    },
    {
    "groupId": 12335,
    "adminId": 52,
    "name": "Appolo Health Group",
    "directorName": "Mrs Fatima",
    "fax": "5556565655",
    "email": "test@test.com",
    "status": 1,
    "address1": "Akshya Nagar 1st Block 1st Cross",
    "districtName": "Berkshire",
    "addedDate": "2020-01-13T04:44:54.000Z",
    "lastModified": "2020-03-14T10:30:47.000Z"
  },
  {
    "groupId": 546,
    "adminId": 52,
    "name": "Highlights Health LLC",
    "directorName": "Mr Jordan",
    "fax": "5556565655",
    "email": "test@test.com",
    "status": 1,
    "address1": "Akshya Nagar 1st Block 1st Cross",
    "districtName": "Berkshire",
    "addedDate": "2020-02-13T04:44:54.000Z",
    "lastModified": "2020-02-14T10:30:47.000Z"
  }
]

below is the Object which i want to filter from above array.
const objectReady = {
  "groupId": "1234",
  "name": "Health",
  "addedDate": "01-02-2020",
  "lastModified": "25-02-2020"
}


What I have tried:

Here what i have tried with the lodash plugin.
JavaScript
_.filter(groupsList, obj => moment(obj.lastModified).format('DD-MM-YYYY') === objectReady.lastModified || obj.name.toLowerCase().includes(objectReady.name.toLowerCase()) ||
                Number(obj.groupId) === Number(objectReady.groupId));


above code works fine just for a single condition,
but for multiple values it is not filtering data.

Expected Result should be :

JavaScript
[{
    "groupId": 1234,
    "adminId": 52,
    "name": "HHLC Health co",
    "directorName": "Mr Daric",
    "fax": "5556565655",
    "email": "test@test.com",
    "status": 1,
    "address1": "Akshya Nagar 1st Block 1st Cross",
    "districtName": "Berkshire",
    "addedDate": "2020-02-01T04:44:54.000Z",
    "lastModified": "2020-02-25T10:30:47.000Z"
    }]
Posted
Updated 26-Feb-20 1:35am

1 solution

No need for a plugin - filter is already built in to Javascript:
Array.prototype.filter() - JavaScript | MDN[^]
JavaScript
let filteredList = groupsList.filter(obj => moment(obj.lastModified).format('DD-MM-YYYY') === objectReady.lastModified || obj.name.toLowerCase().includes(objectReady.name.toLowerCase()) || Number(obj.groupId) === Number(objectReady.groupId));
Demo[^]

The reason it doesn't appear to be filtering anything out is that all three objects in your sample data match one of the conditions in your filter - specifically, all three have a name which contains the word "health".
 
Share this answer
 
Comments
FireMonkey92 26-Feb-20 21:02pm    
Ohky, i got the point. One more doubt,

If i keep the object like...

const objectReady = {
"groupId": "1234",
"name": "",
"addedDate": "01-02-2020",
"lastModified": "25-02-2020"
}
it should jut filter the first object in the array. Instead of returning all thoese three object in an array. How to achive that?
Richard Deeming 27-Feb-20 6:03am    
You'll have to change your filter function to account for the empty name:
let filteredList = groupsList.filter(obj => moment(obj.lastModified).format('DD-MM-YYYY') === objectReady.lastModified || (objectReady.name && obj.name.toLowerCase().includes(objectReady.name.toLowerCase())) || Number(obj.groupId) === Number(objectReady.groupId));
Updated demo[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900