0

In my Rails app I have built a JSON response for a page which I need to search through in my JavaScript.

A small part of my JSON looks like:

{
  "album": {
    "id": 117716,
    "title": "title",
    "description": "",
    "flickr_id": "72157634990883894",
    "collection_id": 3680,
    "photos": [
      {
        "photo": {
          "id": 564585,
          "title": "IMG_18452",
          "description": "",
          "flickr_id": "944346935",
          "flickr_secret": "74e34579046",
          "flickr_server": "72418",
          "flickr_farm": "7",
          "flickr_original_secret": "efdfd1afb65",
          "format": "jpg",
          "width": 1536,
          "height": 2048,
          "source_small_square": "http://farm8.staticflickr.com/74e18/9434508316935_74e3fa7906_s.jpg",
          "source_large_square": "http://farm8.staticflickr.com/774e18/940831456935_74e3fa7906_q.jpg",
          "source_thumbnail": "http://farm8.staticflickr.com/741e8/9408316935_74e3fasfg7906_t.jpg",
          "source_small_240": "http://farm8.staticflickr.com/74r18/940831693dfh5_74e3fa7906_m.jpg",
          "source_small_320": "http://farm8.staticflickr.com/74t18/94083169fgh35_74e3fa7906_n.jpg",
          "source_medium_640": "http://farm8.staticflickr.com/874e18/940t865316935_74e3fa7906_z.jpg",
          "source_medium_800": "http://farm8.staticflickr.com/37418/94083165356935_74e3fa7906_c.jpg",
          "source_large": "http://farm8.staticflickr.com/2746518/940831695635_74e3fa7906_b.jpg",
          "source_original": "http://farm8.staticflickr.com/745618/9408346416935_efe51afb65_o.jpg"
        }
      },
      {
        "photo": {
          <another photo>
        }
      }]
    }}

Basically I have an integer in my JavaScript, like 564585 and need to search through this array somehow to find a photo with an id that matches. What is the best way of doing this?

I found this page and script: http://devcrapshoot.com/javascript/how-to-search-a-javascript-object-array

I gave it a test and it seems to locate things higher up in the object no problem but it cannot locate what I need. But if it did then that script would be perfect.

Any ideas?

cookie monster
  • 10,671
  • 4
  • 31
  • 45
rctneil
  • 7,016
  • 10
  • 40
  • 83
  • I am not convinced about the `marked as duplicate` here. The other question is about accessing a property, this question is about searching – GôTô May 17 '14 at 17:23
  • @GôTô: Unless the OP doesn't know how to do a comparison of values, searching involves accessing properties as described in the other question. – cookie monster May 17 '14 at 17:28
  • @cookiemonster OP said this is a small part of his object. There could be `photo` objects in other places than the ones we see here. OP gave a link to a script that provides search functionnality on objects. – GôTô May 17 '14 at 17:33
  • @GôTô: I'm not sure what you're getting at. What you're describing would illustrate why he needs to know how to do it in a general way rather than in a specific way for the structure presented above. That's what the other Q&A gives. – cookie monster May 17 '14 at 17:36
  • @GoTo. Yes. I stripped this down to show just the Album. There are other items above the Album containing other parts of the page. As you stated, those other items also contain photos but not necessarily within an album. I just need to be able to return each item that contains a key value pair of id and the photo_id I pass in – rctneil May 17 '14 at 17:39
  • @rctneil Good luck going through all your objects then!! – GôTô May 17 '14 at 17:41
  • You can accomplish that applying the knowledge you'll obtain by reading the accepted answer to the other question. It's a very simple matter of reading properties of objects and iterating arrays. If the structure is more complex, then you'll need to write code to deal with that complexity. It isn't hard, but it may take a little thought. If there can be arrays within arrays, then you'll use nested loops. All of this can be done with the most basic and fundamental language features. – cookie monster May 17 '14 at 17:43
  • @cookiemonster Seriously? – GôTô May 17 '14 at 17:50
  • @GôTô: Of course. If one needs to find data in a complex structure, one needs to learn how to do it. It's simple, but the basics must be learned. What's the problem? Do you feel this is too hard for the OP? Because it isn't. – cookie monster May 17 '14 at 17:57
  • @cookiemonster What's the point in using a library for anything then? Just remove jQuery, what does it do that you can't? Still, the main point is: this is not the same question as the one you mentioned as a duplicate – GôTô May 17 '14 at 18:00
  • @GôTô: I have nothing against libraries when used sensibly. But the OP is asking how to locate data in the structure. That's *exactly* what the other question deals with. Only thing missing is how to compare a value, and if he doesn't know how to use `===`, then he definitely needs to start with the basics. – cookie monster May 17 '14 at 18:02
  • ...and jQuery is definitely optional and should only be used on projects where it makes sense. – cookie monster May 17 '14 at 18:03
  • @cookiemonster Meaning nowhere since you can always write the code it contains. Need to make an ajax call? Just go with the xmlHttpRequest object! Why use a library when you can spend hours or days writing your own code? We could have answered rctneil in minutes. – GôTô May 17 '14 at 18:10
  • @GôTô: No, I said *"where it makes sense"*, meaning rational developers can decide for themselves when and where it will be beneficial. If the site requires minimal JavaScript, then likely a knowledgeable developer will forego jQuery. If it requires massive code and old IE support, especially IE6/7, then more likely the developer will decide the inclusion is worth it. If you tend to think in terms of always/never, all/nothing, black/white, then I'd say your thinking is far too narrow. – cookie monster May 17 '14 at 18:21
  • ...And rctneil has already been answered via the link. If you mean we could have written his code for him in minutes, you're right, but he'd be no better off since as you and he pointed out, his structure isn't fully represented above. – cookie monster May 17 '14 at 18:25

0 Answers0