首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何过滤基于大小写文本匹配的动态JSON对象嵌套数组

如何过滤基于大小写文本匹配的动态JSON对象嵌套数组
EN

Stack Overflow用户
提问于 2020-10-21 18:49:38
回答 2查看 538关注 0票数 0

我得到了一个动态的JSON对象嵌套级别数组,JSON对象的键属性每次都是动态的。我需要在动态JSON对象中搜索匹配的文本记录。搜索文本可以是小写,也可以是大写。我需要根据搜索文本过滤JSON对象的数组,即使它不区分大小写,它应该适用于所有类型的嵌套级别的JSON对象的动态数组,并过滤记录。非常感谢。

下面添加了示例动态JSON数据。

代码语言:javascript
运行
复制
[
   {
      "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"
   }
]
EN

回答 2

Stack Overflow用户

发布于 2020-10-22 00:17:09

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

代码语言:javascript
运行
复制
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);

如果你不喜欢输出格式,你可以返回过滤后的原始数组:

代码语言:javascript
运行
复制
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);

票数 1
EN

Stack Overflow用户

发布于 2020-10-21 19:38:39

下面的脚本正在搜索包含搜索文本的键和值。不区分大小写。

然后你的JSON对象就是jsonObjects (顺便说一句: JSON有buggy,它漏掉了一些括号)。我很确定性能是可以优化的,但它是有效的。

代码语言:javascript
运行
复制
let searchText = "child";
searchText = searchText.toLowerCase();
const filteredJsonObjects = jsonObjects.filter(obj => objectContainsString(obj, searchText));

/**
 *  @param {object} obj
 *  @param {string} searchString
 *  @returns {boolean}
 */
function objectContainsString(obj, searchString) {

  if(typeof obj !== "object" || obj === null) {
    return false;
  }

  // Does object has a key named like the searchText?
  // Remove this lines if you don't want to search for the keys too...
  if(Object.keys(obj).find(key => key.toLowerCase().includes(searchText))) {
    return true;
  }
  
  // else check the values
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      const value = obj[key];

      // If object or array call again...
      if(typeof value === "object" && objectContainsString(value, searchString)) {
        return true;
      }
      if(Array.isArray(value) && value.some(valueInArray => objectContainsString(valueInArray, searchString))) {
        return true;
      }
       
      // Else compare...
      if((typeof value === "string" || typeof value === "number") && (value + "").toLowerCase() === searchString) {
        return true;
      }
    }    
  }  
  return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64461968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档