如何在由所有类型的数据类型组成的嵌套对象数组中实现搜索?简而言之,我想在表格中搜索


[

{
"address": {
"address_1": "Jalna",
"address_2": "",
"city": "Jalna",
"province": "Maharashtra",
"province_code": "",
"zip": "757435",
"country": "India",
"country_code": "IN",
"phone": ""
},
"geo_cordinates": {
"title": "Jalna, Maharashtra, India",
"latLng": "19.8346659,75.88163449999999",
"lat": "19.8346659",
"lng": "75.88163449999999"
},
"_id": "630dce41d0abb7a0deed828a",
"title": "MA",
"description": "",
"holdInventory": false,
"storePickupAllowed": false,
"rules": [],
"location_type": "CUSTOM",
"createdAt": "2022-08-30T08:45:53.211Z",
"updatedAt": "2022-08-30T08:45:53.211Z"
},
{
"address": {
"address_1": "Jarul",
"address_2": "",
"city": "Mayurbhanj",
"province": "Odisha",
"province_code": "",
"zip": "757046",
"country": "India",
"country_code": "IN",
"phone": ""
},
"geo_cordinates": {
"title": "Jarul, Odisha 757046, India",
"latLng": "22.4131695,86.09850689999999",
"lat": "22.4131695",
"lng": "86.09850689999999"
},
"_id": "630dce65d0abb7a0deed8296",
"title": "OR",
"description": "",
"holdInventory": false,
"storePickupAllowed": false,
"rules": [],
"location_type": "CUSTOM",
"createdAt": "2022-08-30T08:46:29.695Z",
"updatedAt": "2022-08-30T08:46:29.695Z"
},
{
"address": {
"address_1": "Guwahati",
"address_2": "",
"city": "Kamrup",
"province": "Assam",
"province_code": "",
"zip": "753223",
"country": "India",
"country_code": "IN",
"phone": ""
},
"geo_cordinates": {
"title": "Guwahati, Assam, India",
"latLng": "26.1157917,91.7085933",
"lat": "26.1157917",
"lng": "91.7085933"
},
"_id": "630dce94d0abb7a0deed82a2",
"title": "AS",
"description": "",
"holdInventory": false,
"storePickupAllowed": false,
"rules": [],
"location_type": "CUSTOM",
"createdAt": "2022-08-30T08:47:16.898Z",
"updatedAt": "2022-08-30T08:47:16.898Z"
}
]

考虑上面的嵌套对象数组。如果用户试图搜索例如"用户";75〃;它应该在对象中搜索匹配项(与关键字无关(,并返回所有筛选出的匹配数组对象。

另一个例子是,如果有人想找到所有匹配的对象;ja";,然后搜索应该在整个对象中进行,并在对象的值中搜索以找到匹配项,如果有嵌套对象,则它也应该在其中查找,并在嵌套对象值中匹配搜索到的文本。我希望这是明确的

我的代码:-

recursiveObjArraySearch(locations);
function recursiveObjArraySearch(data) {
recursivObjectSearch(data, (filteredData) => {
console.log(filteredData);//*->Output
});
}
function recursivObjectSearch(data, cb) {
let filtered = data.filter(function (obj) {
return inObjectSearch(obj);
});
cb(filtered);
}
function inObjectSearch(obj) {
return Object.keys(obj).some(function (key) {
if (typeof obj[key] === "string") {
if (((obj[key]).toLowerCase()).includes(searchingText.toLowerCase())) {
return (obj[key]);
}
} else if (typeof obj[key] === "boolean") {
if ((obj[key]).toString() == (searchingText)) {
return (obj[key]);
}
} else if (typeof obj[key] === "object") {
inObjectSearch(obj);
}
if ((obj[key]) instanceof Array) {
recursiveObjArraySearch(obj[key]);
}
})
}

我尝试过这种方式,但我认为它太繁忙了,这也引发了一个错误超过最大调用堆栈大小

谢谢!

导致超出最大调用堆栈

else if (typeof obj[key] === "object") {
inObjectSearch(obj);
}

尝试inObjectSearch(obj[key])

完整代码片段

const locations = [

{
"address": {
"address_1": "Jalna",
"address_2": "",
"city": "Jalna",
"province": "Maharashtra",
"province_code": "",
"zip": "757435",
"country": "India",
"country_code": "IN",
"phone": ""
},
"geo_cordinates": {
"title": "Jalna, Maharashtra, India",
"latLng": "19.8346659,75.88163449999999",
"lat": "19.8346659",
"lng": "75.88163449999999"
},
"_id": "630dce41d0abb7a0deed828a",
"title": "MA",
"description": "",
"holdInventory": false,
"storePickupAllowed": false,
"rules": [],
"location_type": "CUSTOM",
"createdAt": "2022-08-30T08:45:53.211Z",
"updatedAt": "2022-08-30T08:45:53.211Z"
},
{
"address": {
"address_1": "Jarul",
"address_2": "",
"city": "Mayurbhanj",
"province": "Odisha",
"province_code": "",
"zip": "757046",
"country": "India",
"country_code": "IN",
"phone": ""
},
"geo_cordinates": {
"title": "Jarul, Odisha 757046, India",
"latLng": "22.4131695,86.09850689999999",
"lat": "22.4131695",
"lng": "86.09850689999999"
},
"_id": "630dce65d0abb7a0deed8296",
"title": "OR",
"description": "",
"holdInventory": false,
"storePickupAllowed": false,
"rules": [],
"location_type": "CUSTOM",
"createdAt": "2022-08-30T08:46:29.695Z",
"updatedAt": "2022-08-30T08:46:29.695Z"
},
{
"address": {
"address_1": "Guwahati",
"address_2": "",
"city": "Kamrup",
"province": "Assam",
"province_code": "",
"zip": "753223",
"country": "India",
"country_code": "IN",
"phone": ""
},
"geo_cordinates": {
"title": "Guwahati, Assam, India",
"latLng": "26.1157917,91.7085933",
"lat": "26.1157917",
"lng": "91.7085933"
},
"_id": "630dce94d0abb7a0deed82a2",
"title": "AS",
"description": "",
"holdInventory": false,
"storePickupAllowed": false,
"rules": [],
"location_type": "CUSTOM",
"createdAt": "2022-08-30T08:47:16.898Z",
"updatedAt": "2022-08-30T08:47:16.898Z"
}
];

const searchingText = "CUSTOM";
recursiveObjArraySearch(locations);
function recursiveObjArraySearch(data) {
recursivObjectSearch(data, (filteredData) => {
console.log(filteredData);//*->Output
});
}
function recursivObjectSearch(data, cb) {
let filtered = data.filter(function (obj) {
return inObjectSearch(obj);
});
cb(filtered);
}
function inObjectSearch(obj) {
return Object.keys(obj).some(function (key) {
if (typeof obj[key] === "string") {
if (((obj[key]).toLowerCase()).includes(searchingText.toLowerCase())) {
return (obj[key]);
}
} else if (typeof obj[key] === "boolean") {
if ((obj[key]).toString() == (searchingText)) {
return (obj[key]);
}
} else if (typeof obj[key] === "object") {
inObjectSearch(obj[key]);
}
if ((obj[key]) instanceof Array) {
recursiveObjArraySearch(obj[key]);
}
})
}

最新更新