首页 > 解决方案 > 如何根据大写和小写的文本匹配过滤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"
   }
]

标签: javascriptjsonreactjsecmascript-6ecmascript-5

解决方案


此解决方案不会改变您的原始数组。它将在数组元素中搜索深层嵌套对象的值。它甚至会返回搜索结果在您的数组中的位置。如果没有找到,将返回空对象。它搜索不区分大小写。

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);


推荐阅读