使用Javascript在JSON中查找值

时间:2018-08-15 17:24:21

标签: javascript json search lookup

我是Java的新手,已经获得了一项任务。我有一个JSON供稿,如下所示:

var data = {
  "feeds": {
    "regions": [{
        "name": "Lichtenberg",
        "id": "01408.b",
        "suburbs": [{
            "name": "Fennpfuhl",
            "views": 76400
          },
          {
            "name": "Lichtenberg",
            "views": 87895
          },
          {
            "name": "Rummelsberg",
            "views": 10239
          }
        ]
      },
      {
        "name": "Mitte",
        "id": "03442.f",
        "suburbs": [{
            "name": "Tiergarten",
            "views": 82695
          },
          {
            "name": "Mitte",
            "views": 67234
          },
          {
            "name": "Hansaviertel",
            "views": 10848
          },
          {
            "name": "Moabit",
            "views": 67500
          }
        ]
      },
      {
        "name": "Friedrichshain-Kreuzberg",
        "id": "01991.o",
        "suburbs": [{
            "name": "Friedrichshain",
            "views": "98494"
          },
          {
            "name": "Kreuzberg",
            "views": "27800"
          }
        ]
      },
      {
        "name": "Templehof-Schöneberg",
        "id": "01778.k",
        "suburbs": [{
            "name": "Friedenau",
            "views": 76595
          },
          {
            "name": "Schöneberg",
            "views": 20731
          },
          {
            "name": "Templehof",
            "views": 58000
          },
          {
            "name": "Mariendorf",
            "views": 32300
          }
        ]
      },
      {
        "name": "Pankow",
        "id": "02761.q",
        "suburbs": [{
            "name": "Wießensee",
            "views": 81294
          },
          {
            "name": "Prenzlauer Berg",
            "views": 76470
          },
          {
            "name": "Pankow",
            "views": 90210
          }
        ]
      }
    ],
    "branding": [{
      "municipality_id": "01408.b",
      "brand_color": "#f9cd90"
    }, {
      "municipality_id": "03442.f",
      "brand_color": "#F28123"
    }, {
      "municipality_id": "01991.o",
      "brand_color": "#D34E24"
    }, {
      "municipality_id": "01778.k",
      "brand_color": "#563F1B"
    }, {
      "municipality_id": "02761.q",
      "brand_color": "#38726C"
    }],
    "customer": {
      "name": "Viktoria Tiedemann",
      "date_of_birth": "1981-09-19",
      "address": {
        "street": "Schönfließer Str 9",
        "suburb": "Prenzlauer Berg",
        "postcode": "10439"
      }
    }
  }
};

任务很简单-查找Viktoria Tiedemann的郊区和地区。到目前为止,我尝试使用以下方法:

var customer_suburb;
var customer_name = 'Viktoria Tiedemann';
for (var i = 0; i < data.feeds.customer.length; i++){
  if (data.feeds.customer.name[i] == customer_name){
    customer_suburb = data.feeds.customer.address.suburb;
  }
}

但是它一直在返回未定义的值-我在哪里出错?我正在考虑使用相同的过程来获取区域。

1 个答案:

答案 0 :(得分:2)

您的数据中只有一个客户,而不是一系列客户,可以通过以下方式访问该客户:

data.feeds.customer

如果 customer应该是一个数组,则可以找到带有以下内容的Viktoria:let customer = data.feeds.customer.find(c => c.name === 'Viktoria Tiedemann')

您可以通过以下方式前往郊区:

let suburb = data.feeds.customer.address.suburb

一旦有了,您只需要find()的{​​{1}}数组具有此区域的区域。为此,将suburbsfind()结合起来可以做到这一点:

some()