Maison  >  Questions et réponses  >  le corps du texte

Rechercher des objets Json à l'aide des opérateurs "AND", "OR"

J'ai un problème où je dois rechercher un objet JSON à l'aide d'une chaîne d'opérateur conditionnelle.

L'objet JSON ressemble à

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'}  
]

et la chaîne de condition indiquée ci-dessous..

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

Pas de chance avec Internet... Comment implémenter cela, je ne peux pas l'apporter à SQL Server car cela doit être fait en javascript.

J'ai cherché en ligne pour diviser la chaîne de condition en un tableau divisé par opérateur (AND/OR) et recherché dans l'objet de données json un par un, mais la chaîne de condition est différente pour chaque requête. Et travaillez sur l’implémentation correcte des opérateurs (AND/OR). Je ne suis pas sûr que ce soit la meilleure idée

J'ai besoin d'aide pour résoudre ce problème afin de satisfaire tout type de condition fournie à l'aide de plusieurs supports imbriqués.

Merci

P粉029327711P粉029327711409 Il y a quelques jours596

répondre à tous(1)je répondrai

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

    répondre
    0
  • Annulerrépondre