javascript - 如何根据大写和小写的文本匹配过滤JSON对象的动态嵌套数组
问题描述
我得到一个 JSON 对象的动态嵌套级别数组,JSON 对象键属性每次都是动态的。我需要在动态 JSON 对象中搜索匹配的文本记录。搜索文本可以是小写或大写。我需要根据搜索文本过滤 JSON 对象数组,即使它不区分大小写,它也应该适用于所有类型的 JSON 对象的嵌套级别动态数组并过滤记录。非常感谢。
下面添加了示例动态 JSON 数据。
[
{
"businessEntityName":{
"businessEntityName":"abc1 ",
"businessEntityDescription":"welcome to the abcd"
},
"name":"Paul",
"applicationName":{
"applicationRoleOrGroupName":"view",
"applicationRoleOrGroupDescription":"Viewers on view"
},
"status":{
"name":"Removed on: 27-Aug-2020",
"style":"error"
},
"type":"Manager"
},
{
"businessEntityName":{
"businessEntityName":"Internal",
"businessEntityDescription":"Okay"
},
"name":"John Smith",
"applicationRoleOrGroupName":{
"applicationRoleOrGroupName":"Master",
"applicationRoleOrGroupDescription":"Can access read only information of the non-sensitive pages"
},
"status":{
"name":"Active from: 26-Aug-2020",
"style":"success"
},
"type":"admin"
},
{
"businessEntityName":{
"businessEntityName":"External",
"businessEntityDescription":"All my Data"
},
"name":"ramesh",
"applicationRoleOrGroupName":{
"applicationRoleOrGroupName":"welcome",
"applicationRoleOrGroupDescription":"User for My data"
},
"status":{
"name":"Active from: 18-Aug-2020",
"style":"success"
},
"type":"HOD"
}
]
解决方案
此解决方案不会改变您的原始数组。它将在数组元素中搜索深层嵌套对象的值。它甚至会返回搜索结果在您的数组中的位置。如果没有找到,将返回空对象。它搜索不区分大小写。
const arr = [{
"businessEntityName": {
"businessEntityName": "abc1 ",
"businessEntityDescription": "welcome to the abcd"
},
"name": "Paul",
"applicationName": {
"applicationRoleOrGroupName": "view",
"applicationRoleOrGroupDescription": "Viewers on view"
},
"status": {
"name": "Removed on: 27-Aug-2020",
"style": "error"
},
"type": "Manager"
},
{
"businessEntityName": {
"businessEntityName": "Internal",
"businessEntityDescription": "Okay"
},
"name": "John Smith",
"applicationRoleOrGroupName": {
"applicationRoleOrGroupName": "Master",
"applicationRoleOrGroupDescription": "Can access read only information of the non-sensitive pages"
},
"status": {
"name": "Active from: 26-Aug-2020",
"style": "success"
},
"type": "admin"
},
{
"businessEntityName": {
"businessEntityName": "External",
"businessEntityDescription": "All my Data"
},
"name": "ramesh",
"applicationRoleOrGroupName": {
"applicationRoleOrGroupName": "welcome",
"applicationRoleOrGroupDescription": "User for My data"
},
"status": {
"name": "Active from: 18-Aug-2020",
"style": "success"
},
"type": "HOD"
}
]
let findInObject = (obj, str) => {
let result = JSON.parse(JSON.stringify(obj));
const re = new RegExp(str, "gi"); //regex to match global case insensitive
Object.keys(result).map(function(key, index) {
if (typeof(result[key]) === "string" && result[key].match(re)) { //add here if you don't want to search for style: && key != "style"
result[key] = true;
} else if (result[key] != undefined && result[key] != null && typeof(result[key]) === "object" && Object.keys(result[key]).length != 0) {
result[key] = findInObject(result[key], str);
if (Object.keys(result[key]).length === 0 && obj.constructor === Object) {
delete result[key];
}
} else {
delete result[key];
}
});
return result;
}
let result = arr.map((obj) => findInObject(obj, 'ma')); //Enter you search text here
console.log(result);
如果您不喜欢输出格式,您可以返回过滤后的原始数组:
const arr = [{
"businessEntityName": {
"businessEntityName": "abc1 ",
"businessEntityDescription": "welcome to the abcd"
},
"name": "Paul",
"applicationName": {
"applicationRoleOrGroupName": "view",
"applicationRoleOrGroupDescription": "Viewers on view"
},
"status": {
"name": "Removed on: 27-Aug-2020",
"style": "error"
},
"type": "Manager"
},
{
"businessEntityName": {
"businessEntityName": "Internal",
"businessEntityDescription": "Okay"
},
"name": "John Smith",
"applicationRoleOrGroupName": {
"applicationRoleOrGroupName": "Master",
"applicationRoleOrGroupDescription": "Can access read only information of the non-sensitive pages"
},
"status": {
"name": "Active from: 26-Aug-2020",
"style": "success"
},
"type": "admin"
},
{
"businessEntityName": {
"businessEntityName": "External",
"businessEntityDescription": "All my Data"
},
"name": "ramesh",
"applicationRoleOrGroupName": {
"applicationRoleOrGroupName": "welcome",
"applicationRoleOrGroupDescription": "User for My data"
},
"status": {
"name": "Active from: 18-Aug-2020",
"style": "success"
},
"type": "HOD"
}
]
let findInObject = (obj, str) => {
let result = JSON.parse(JSON.stringify(obj));
const re = new RegExp(str, "gi"); //regex to match global case insensitive
Object.keys(result).map(function(key, index) {
if (typeof(result[key]) === "string" && result[key].match(re)) { //add here if you don't want to search for style: && key != "style"
result[key] = true;
} else if (result[key] != undefined && result[key] != null && typeof(result[key]) === "object" && Object.keys(result[key]).length != 0) {
result[key] = findInObject(result[key], str);
if (Object.keys(result[key]).length === 0 && obj.constructor === Object) {
delete result[key];
}
} else {
delete result[key];
}
});
return result;
}
let result = arr.map((obj) => findInObject(obj, 'ma')); //Enter you search text here
let originalFormat = result.map((obj, i) => Object.keys(obj).length ? arr[i] : {}).filter((obj) => Object.keys(obj).length)
console.log(originalFormat);
推荐阅读
- python - 将 DataFrame 列转换为 json 列
- c - 当行超过 100 个字符时,fprintf 会生成一个 utf-16 文本文件
- css - 如何防止 Outlook 中 HTML 模板中的图像使用 React 进行不必要的扩展?
- flutter - 使用 .aar 模块的 Flutter 插件在示例应用程序中构建和运行良好,但无法在其他应用程序中构建
- java - 我写了一个二分搜索,但有一些错误
- drake - MultiBodyPlant 上下文中带有 nans 的额外主体在模拟时导致错误
- python - 部分可搜索的 pdf 文档
- javascript - Highcharts 股票图表,系列填充?
- pact - Pact.Net 是否支持 https 验证?
- r - 参数上的 R 函数条件