使用'AND”、'OR”运算符搜索 Json 对象
P粉029327711
P粉029327711 2023-09-06 10:23:16
0
1
631

我遇到一个问题,需要使用条件运算符字符串搜索 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)。我不确定这是最好的主意

我需要帮助如何解决这个问题以满足使用多个嵌套括号提供的任何类型的条件。

谢谢

P粉029327711
P粉029327711

全部回复(1)
P粉174151913

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';
}));
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板