3gwebtrain
3gwebtrain

Reputation: 15303

Filtering array of objects using includes() not working

I am trying to do a filter using contains. but not works. any one figureout the issue here.

const nestedFilter = (targetArray, filters) => {
  var filterKeys = Object.keys(filters);
  return targetArray.filter(function(eachObj) {
    return filterKeys.some(function(eachKey) {
      return eachObj[eachKey].includes(filters[eachKey]);
    });
  });
};

let filter1 = {Id: "24", Name: "Api Gateway"};
let products = [
  {
    "Id": 100024,
    "Name": "Api Gateway UpdateXX",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "B, Mohamedarif (Cognizant)",
    "CreatedDate": "2019-21-17T06:35:20.107Z"
  },
  {
    "Id": 100026,
    "Name": "Chennai became cold very soon!!",
    "Description": "stridfgdfgng",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100031,
    "Name": "Soma Banerjee",
    "Description": "Updated for Testing2",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100032,
    "Name": "Soma Banerjee",
    "Description": "Soma Banerjee",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100033,
    "Name": "Update Same Data Contact",
    "Description": "Update Same Data Contact",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100034,
    "Name": "New",
    "Description": "New Des",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100039,
    "Name": "100001 New111",
    "Description": "100001 New",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100041,
    "Name": "3 way entry",
    "Description": "3 way entry",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100042,
    "Name": "3 way entry1",
    "Description": "3 way entry",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100043,
    "Name": "ghgjhlk",
    "Description": "sdfsfs",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100044,
    "Name": "ghgjhlk",
    "Description": "sdfsfs",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100045,
    "Name": "Azure New",
    "Description": "Azure New",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100047,
    "Name": "2 way",
    "Description": "2 way",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100049,
    "Name": "C2-new",
    "Description": "C2-new",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100050,
    "Name": "C0001-new",
    "Description": "C00012-new",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100051,
    "Name": "Common Add",
    "Description": "Common Add",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100053,
    "Name": "Mama",
    "Description": "Mama",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100054,
    "Name": "New title",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100055,
    "Name": "CTS",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100056,
    "Name": "New course added",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100057,
    "Name": "Chennai 2020",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100058,
    "Name": "updated Course",
    "Description": "Updated Course",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100059,
    "Name": "New Coursegfdgdfg",
    "Description": "New Course",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100061,
    "Name": "New Cosmos",
    "Description": "Cosmos",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100062,
    "Name": "New Cosmos2222",
    "Description": "Cosmos222",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100063,
    "Name": "New Cosmosooooo",
    "Description": "Cosmoooooo",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100064,
    "Name": "New Cosmo123",
    "Description": "Cosmo123",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100066,
    "Name": "New dfsfsfsd",
    "Description": "Cosmosdfsfsfds123",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100068,
    "Name": "Using good",
    "Description": "strisdfsfsdfng",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100077,
    "Name": "Nadu Nadu",
    "Description": "NaduNadu",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100078,
    "Name": "Course 0013 for Albanian literature",
    "Description": "Course 0012 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100079,
    "Name": "Course 0002 for Albanian literature",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100080,
    "Name": "Course 0003 for Albanian literature",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100081,
    "Name": "a-z",
    "Description": "a-z",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100082,
    "Name": "Course 0004 dipa",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100083,
    "Name": "Update",
    "Description": "Up",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100084,
    "Name": "fsdfsfsfs",
    "Description": "ssdfsdfsftrisdfsdfng",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100086,
    "Name": "fsdfsfsfs",
    "Description": "ssdfsdfsftrisdfsdfng",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100087,
    "Name": "dsdsdfsfsdfs",
    "Description": "strdfsdfsdfsfsding",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100088,
    "Name": "dsdsdfsfsdfs",
    "Description": "strdfsdfsdfsfsding",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100089,
    "Name": "dsdsdfsfsdfs",
    "Description": "strdfsdfsdfsfsding",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100090,
    "Name": "dsdsdfsfsdfs",
    "Description": "strdfsdfsdfsfsding",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100091,
    "Name": "100001",
    "Description": "st100001ring",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100092,
    "Name": "100001",
    "Description": "st100001ring",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100093,
    "Name": "fdgdfdgdgdfgdf",
    "Description": "st1000dsfgddfgdg01ring",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100095,
    "Name": "testservice",
    "Description": "LogicAppVS",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  },
  {
    "Id": 100096,
    "Name": "TestingLogicApp",
    "Description": "VisualSTudio",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
  }
]

console.log(nestedFilter(products, filter1));//getting error.

looking the result:

{
    "Id": 100024,
    "Name": "Api Gateway UpdateXX",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "B, Mohamedarif (Cognizant)",
    "CreatedDate": "2019-21-17T06:35:20.107Z"
  }

thanks in advance.

Live Demo

Upvotes: 0

Views: 848

Answers (1)

Mohammad Usman
Mohammad Usman

Reputation: 39322

You are getting the following error in console:

eachObj[eachKey].includes is not a function

This error is appearing because one of the values in each object is of type Number in your array. The method used in code i.e String.prototype.includes() is available for String type and won't work on type Number and will throw above error.

To fix this you need to convert values of type Number to string using .toString() method available on Number.ptototype before using .includes():

let filter = {Id: "24", Name: "Api Gateway"};
let data = [{
    "Id": 100024,
    "Name": "Api Gateway UpdateXX",
    "Description": "Course 0002 for Albanian literature",
    "CreatedBy": "B, Mohamedarif (Cognizant)",
    "CreatedDate": "2019-21-17T06:35:20.107Z"
}, {
    "Id": 100026,
    "Name": "Chennai became cold very soon!!",
    "Description": "stridfgdfgng",
    "CreatedBy": "Script",
    "CreatedDate": "2019-04-17T06:35:20.107Z"
}];

let resultBy = (targetArr, filterObj) => {
  let keys = Object.keys(filterObj);
  
  return targetArr.filter(
    o => keys.every(key => o[key].toString().includes(filterObj[key]))
  );
};

console.log(resultBy(data, filter));

Upvotes: 1

Related Questions