Lazarius
Lazarius

Reputation: 53

Lookup Value in JSON using Javascript

I'm very new to Javascript and have been given a task. I have a JSON feed that has been given as follows:

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"
      }
    }
  }
};

The task is simple - to find the suburb and the region of Viktoria Tiedemann. So far I've tried using the below:

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

but it keeps on returning undefined values - where am I going wrong? I'm thinking to use the same process to get the region.

Upvotes: 1

Views: 6296

Answers (1)

Mark
Mark

Reputation: 92460

In your data, you have just one customer — not an array of customers — which is accessible with:

data.feeds.customer

( if customer was supposed to be an array, you could find Viktoria with : let customer = data.feeds.customer.find(c => c.name === 'Viktoria Tiedemann') )

You can get the suburb with:

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

Once you have that, you just need to find() the region whose suburbs array has this. For that find() combined with some() does this succinctly:

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"}}}};

let suburb = data.feeds.customer.address.suburb
let region = data.feeds.regions.find(region => region.suburbs.some(s => s.name === suburb))

console.log("suburb:", suburb, "region:", region.name)

Upvotes: 4

Related Questions