J'ai un JSON comme ci-dessous. J'ai besoin de trouver la valeur de name, dans ce cas, ce sera "Mark". Je ne sais pas à quelle profondeur l'objet se trouve dans la structure JSON ou combien d'entre eux dans le JSON. Il pourrait y en avoir plusieurs à différents niveaux. Je sais que je peux simplement parcourir chaque objet de valeur pour le rechercher. Existe-t-il un moyen plus simple?

{
  "person": {
    "value": [
      {
        "type": "RANGE",
        "value": {
          "to": "2019-11-05T05:59:59.999Z",
          "from": "1900-01-01T06:00:00.000Z"
        },
        "field": "published",
        "include": true
      },
      {
        "type": "AND",
        "value": [
          {
            "type": "OR",
            "value": [
              {
                "type": "AND",
                "value": [
                  {
                    "type": "OR",
                    "include": true,
                    "value": [
                      {
                        "type": "MATCH",
                        "value": "Mark",
                        "field": "name",
                        "include": true
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "MATCH",
            "value": "37",
            "field": "age",
            "include": true
          }
        ],
        "include": true
      }
    ]
  }
}
1
topcan5 4 nov. 2019 à 21:34

1 réponse

Meilleure réponse

C'est une manière récursive simple de trouver "Mark":

function runThroughObject(object) {
  for (const key in object) {
    const value = object[key]

    if (typeof value === 'object' && value !== null) {
      runThroughObject(value)
    }
    else if (value === 'name') {
      console.log(object.value)
    }
  }
}

Voici l'extrait de travail complet:

const data = {
  "person": {
    "value": [
      {
        "type": "RANGE",
        "value": {
          "to": "2019-11-05T05:59:59.999Z",
          "from": "1900-01-01T06:00:00.000Z"
        },
        "field": "published",
        "include": true
      },
      {
        "type": "AND",
        "value": [
          {
            "type": "OR",
            "value": [{
              "type": "AND",
              "value": [{
                "type": "OR",
                "include": true,
                "value": [{
                  "type": "MATCH",
                  "value": "Mark",
                  "field": "name",
                  "include": true
                }]
              }]
            }]
          },
          {
            "type": "MATCH",
            "value": "37",
            "field": "age",
            "include": true
          }
        ],
        "include": true
      }
    ]
  }
}

function runThroughObject(object) {
  for (const key in object) {
    const value = object[key]

    if (typeof value === 'object' && value !== null) {
      runThroughObject(value)
    }
    else if (value === 'name') {
      console.log(object.value)
    }
  }
}

runThroughObject(data)
1
Vl4dimyr 4 nov. 2019 à 19:00