我正在尝试使用contains
进行过滤。但不起作用。任何人都可以在这里找到问题。
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.
查看结果:
{
"Id": 100024,
"Name": "Api Gateway UpdateXX",
"Description": "Course 0002 for Albanian literature",
"CreatedBy": "B, Mohamedarif (Cognizant)",
"CreatedDate": "2019-21-17T06:35:20.107Z"
}
提前谢谢。
答案 0 :(得分:0)
您在控制台中遇到以下错误:
eachObj [eachKey] .includes不是函数
出现此错误是因为每个对象中的值之一在您的数组中为Number
类型。代码String.prototype.includes()
中使用的方法适用于String
类型,不适用于Number
类型,并且会抛出上述错误。
要解决此问题,您需要先使用Number
上可用的.toString()
方法将Number.ptototype
类型的值转换为字符串,然后再使用.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));