使用“AND”、“OR”运算符搜索Json对象

dxxyhpgq  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(134)

我面临一个问题,我需要搜索一个JSON对象与条件运算符字符串。
JSON对象看起来像

let Data = [
  {Application: 'APIM', Owner: 'Vande Mataram','Customer Code': 'DJLFKASJF',Environment: 'Dev',OS: 'Linux',Region: 'EU','TEAM ': 'DemoTestCOE'},
  {TEAM: 'BCC'},
  {Team: 'DemoTestCOE','Customer Code': 'DJLFKASJF',Owner: 'Vande Mataram',Environment: 'Prod',Region: 'EU',Application: 'Ui-Path',OS: 'Windows'},
  {Application: 'MGMT','Customer Code': 'DJLFKASJF', os: 'Windows','Owner ': 'Vande Mataram', Region: 'East US 2',Team: 'DemoTestCOE',Environment: 'MGMT'  },
  {Application: 'PIER',Owner: 'Govindarajulu Balaji',OS: 'Windows',Region: 'DemoTestCOE', Environment: 'QA'}  
]

字符串
和如下的条件字符串..

let condition =
  '"Owner": "Vande Mataram" AND ("Application": "APIM" AND "Customer Code": "DJLFKASJF") OR ("Owner": "Vande Mataram" AND "Customer Code": "DJLFKASJF") OR "Owner": "Vande Mataram"'


没有运气与互联网…如何实现这一点,我不能把它带到sql server,因为它需要在JavaScript中完成。
我在互联网上搜索将条件字符串拆分为一个数组,通过运算符(AND/OR)拆分,并在json数据对象中逐个搜索,但条件字符串因每个请求而异。并努力正确地实现运算符(AND/OR)。我不确定这是个好主意
我需要帮助如何处理这一点,以满足任何类型的条件提供了多个和嵌套括号。
谢了

b4qexyjb

b4qexyjb1#

Data只是一个对象数组,因此可以使用Array.filter

let Data = [
  {Application: 'APIM', Owner: 'Vande Mataram','Customer Code': 'DJLFKASJF',Environment: 'Dev',OS: 'Linux',Region: 'EU','TEAM ': 'DemoTestCOE'},
  {TEAM: 'BCC'},
  {Team: 'DemoTestCOE','Customer Code': 'DJLFKASJF',Owner: 'Vande Mataram',Environment: 'Prod',Region: 'EU',Application: 'Ui-Path',OS: 'Windows'},
  {Application: 'MGMT','Customer Code': 'DJLFKASJF', os: 'Windows','Owner ': 'Vande Mataram', Region: 'East US 2',Team: 'DemoTestCOE',Environment: 'MGMT'  },
  {Application: 'PIER',Owner: 'Govindarajulu Balaji',OS: 'Windows',Region: 'DemoTestCOE', Environment: 'QA'}  
]
// and the condition string like below..
/*
let condition =
  '"Owner": "Vande Mataram" AND ("Application": "APIM" AND "Customer Code": "DJLFKASJF") OR ("Owner": "Vande Mataram" AND "Customer Code": "DJLFKASJF") OR "Owner": "Vande Mataram"'
*/
console.log(Data.filter(item => {
    return item.Owner === 'Vande Mataram' 
    && (item.Application === "APIM" && item['Customer Code'] === "DJLFKASJF") 
    || (item.Owner === 'Vande Mataram' && item['Customer Code'] === "DJLFKASJF")
    || item.Owner === 'Vande Mataram';
}));

字符串

相关问题