搜索

首页  >  问答  >  正文

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

我遇到一个问题,需要使用条件运算符字符串搜索 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粉029327711P粉029327711452 天前645

全部回复(1)我来回复

  • P粉174151913

    P粉1741519132023-09-07 00:08:39

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

    回复
    0
  • 取消回复