0

I want to loop though all values under services_raw_nl and get the name and number belonging to thoses values, e.g. In-house Catering and 9 and Live music and 6

I want to use a for-in loop, but I have 2 problems:

  1. the problem is that the name of services_raw_nl may also be services_raw_en (depending on the chosen language of the user).
  2. I'm unsure how to access the properties and their values. Basically I just want to get the name and value of a property under services_raw_nl by their index.

I wanted to make this available as a JSFiddle, but don't know how to make the JSON data available there and since this service is not yet live I can not call it from JSFiddle.

{
  "responseHeader":{
    "status":0,
    "QTime":1,
    "params":{
      "facet":"true",
      "sort":"membervalue desc",
      "fl":"id,city,thumb",
      "facet.mincount":"1",
      "indent":"on",
      "start":"0",
      "q":["*:*",
        "*:*"],
      "facet.field":["country_raw_nl",
        "services_raw_nl",
        "city"],
      "wt":"json",
      "fq":"country_nl:=australie",
      "rows":"12"}},
  "response":{"numFound":10,"start":0,"docs":[
      {
        "id":"842",
        "city":"whitsundays",
        "thumb":"735_739_CEREMONY-PAVILLION-2.jpg"}]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "country_raw_nl":[
        "Australie",10],
      "services_raw_nl":[
        "In-house Catering",9,
        "Live music",6],
      "partylocation":[
        "true",8,
        "false",2],
      "hasphoto":[
        "true",9,
        "false",1],
      "hasvideo":[
        "false",10],
      "rating":[
        "0.0",10],
      "rating_rounded":[
        "0.0",10],
    "facet_dates":{},
    "facet_ranges":{}}}

Here's the loop I'm trying:

for (var service in response.facet_counts.facet_fields.services_raw_nl) {
    console.log(response.facet_counts.facet_fields.services_raw_nl[service].???);
}
Adam
  • 6,041
  • 36
  • 120
  • 208
  • 2
    It looks like `services_raw_nl` is an **array** not an object: `["In-house Catering",9,"Live music",6]`, in which case you should use a normal `for` loop, not `for...in`. I assume you know how to iterate over arrays? – Felix Kling Jan 20 '14 at 18:11
  • 1
    If 'In-house Catering' is paired with '9' then ideally they would comprise a single object like `{ name: 'In-house Catering', value: 9 }`, and you would have an array of these objects that you could then loop over. – Andy Jan 20 '14 at 18:11
  • 2
    @Andy: Having an object of the form `{"In-house Catering": 9, "Live music": 6}` might make more sense. – Felix Kling Jan 20 '14 at 18:15
  • @FelixKling: you are right, it's an array, but how would you solve the fact that the name of the array is dynamic? e.g. with postfix `_nl` or `_en`? – Adam Jan 20 '14 at 18:23
  • It looks like the specific property names are contained in the `facet.field` property: `["country_raw_nl", "services_raw_nl", "city"]`. So you could just iterate over that array and get the string that starts with `services_raw` and use it as property name. – Felix Kling Jan 20 '14 at 18:26

1 Answers1

1

You can cache service_raw_nl/en to a variable, and use a simple for loop:

var lang = 'nl', // or 'en', this you probably have stored somewhere in your preceding code?
    services_raw = response.facet_counts.facet_fields['services_raw_' + lang],
    n;
for (n = 0; n < services_raw.length; n++) {
    console.log(services_raw[n]);
}

EDIT

This function returns you an object containig the wanted values.

var lang = 'nl';
function getServices (response, lang) {
    var services_raw = response.facet_counts.facet_fields['services_raw_' + lang],
        n, temp = {};
    for (n = 0; n < services_raw.length; n+= 2) {
        temp[services_raw[n]] = services_raw[n + 1];
    }
    return temp;
}
console.log(getServices(response, lang));

A live demo at jsFiddle.


EDIT II

A simple way to create some HTML from the values of services_raw_nl would be something like this:

var lang = 'nl',
    services_raw = response.facet_counts.facet_fields['services_raw_' + lang],
    n, outer, inner;
for (n = 0; n < services_raw.length; n += 2) {
    outer = document.createElement('span');
    outer.appendChild(document.createTextNode(services_raw[n]));
    inner = outer.appendChild(document.createElement('span'));
    inner.appendChild(document.createTextNode(services_raw[n + 1]));
    what_ever_element.appendChild(outer);
}
Teemu
  • 22,918
  • 7
  • 53
  • 106
  • Ah, thanks! Your code retrieves the title and value, but how can I access them separately and assign those values to a title and count variable respectively? e.g. `In-house Catering` and `9`. Thanks! – Adam Jan 20 '14 at 18:47
  • I'm not sure what you're asking now. The code above gives you "`...the name and value of a property under services_raw_nl by their index."`. Maybe you want to create an object? I'll edit the answer... – Teemu Jan 20 '14 at 18:51
  • I guess I'm unsure what this line does `console.log(services_raw[n])`, because I see in my console the name of the property and on the next line the value with that property. I'd say that the aforementioned line only prints the name+value. I don't need an object but expected to see code that prints 2 console lines: 1 for the name of the property and 1 for the value, once I have that I can use the values elsewhere. Is it clear what is unclear to me? :) – Adam Jan 20 '14 at 19:11
  • `getServices()` returns an object. You've four values in `services_raw_nl` array, you can't put them into two variables without using either an array or an object. Which way exactly you want the values from `services_raw_nl`? [`getServices()`](http://jsfiddle.net/pL3RJ/4/) can return also an array of objects... – Teemu Jan 20 '14 at 19:28
  • Want I need is to use the name and value to build a span for each result, so for `Live music, 6`, I would need to generate a link: `Live music (6)`, so you see I need to loop through the results and get the name and value to build a separate string. – Adam Jan 20 '14 at 19:39
  • 1
    You can use the fiddle in my last comment for that. Or just create an array, and iterate that in the same way, as I've iterated `services_raw` in my second snippet (`n += 2`). – Teemu Jan 20 '14 at 19:48