我得到了一个动态的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"
}
]
发布于 2020-10-22 00:17:09
此解决方案不会改变您的原始数组。它将在数组的元素中搜索深度嵌套对象的值。它甚至返回了搜索结果在数组中的位置。如果什么都没有找到,则返回空对象。它搜索不区分大小写的。
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);
发布于 2020-10-21 19:38:39
下面的脚本正在搜索包含搜索文本的键和值。不区分大小写。
然后你的JSON对象就是jsonObjects
(顺便说一句: JSON有buggy,它漏掉了一些括号)。我很确定性能是可以优化的,但它是有效的。
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;
}
https://stackoverflow.com/questions/64461968
复制相似问题