0

I am struggling with the following:

I have a JSON feed with my data (https://api.myjson.com/bins/1sz7s). I iterate through all ReportItems in this object through javascript / jquery.

Problem I am having is I want to fetch 'records' that contain a specific value, but without knowing the key's name. Basically some kind of if_exists(ID):

$.each(tabledata.ReportItems, function (key, val){
    var ID = "value";
    if (ID in ReportItems) {
        alert("Found!");
    };

It's easy when you know the keyname:

$.each(tabledata.ReportItems, function (key, val){
    var ID = "value";
    if (ID == val.keyname) {
        alert("Found!");
    };
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
lecoque
  • 33
  • 5
  • Are you looking for `for( var i in object ){}`? `i` will be the key, and `object[i]` the value. – kosmos May 29 '15 at 08:12
  • http://stackoverflow.com/questions/5113847/accessing-elements-of-json-object-without-knowing-the-key-names Seems to be duplicate. – Dreamweaver May 29 '15 at 08:13
  • I did see that post, however it utilizes a for...in loop. Isn't $.each the query equivalent? If yes, maybe I can try that route but had no luck trying that suggestion. – lecoque May 29 '15 at 08:16
  • So when I do this: `for (var key in tabledata.ReportItems) { console.log("Key: " + key); console.log("Value: " + tabledata.ReportItems[key]); }` I get: Key : 3 Value : object Object – lecoque May 29 '15 at 08:32
  • Then you should iterate through the next object. `for( var i in object ){ for( var j in object[i] ){ ... } }`. It's recommended the use of `.hasOwnProperty()` to avoid unwanted results (this method does not check down the object's prototype chain). – kosmos May 29 '15 at 09:14

2 Answers2

0

Since you don't already know keynames, you can iterate through val properties to match value

$.each(tabledata.ReportItems, function (key, val){
   var ID = "value";
   for (var keyname in val) {
       if (ID == val[keyname]) {
          alert("Found!");
       }
   }
});
RajeshK
  • 459
  • 5
  • 10
0

I guess this is what you are asking for:

var a = {"ReportItems":[{"Id":"cf92aefb-b857-49ce-b568-722329377e5d","CampaignId":"384f5c87-f8c7-43a6-9deb-76340dcf4cd4","CompanyId":"3ae13b97-7a09-4342-8953-c1d5a55687db","Name":"Item_7699","Price":1577.0,"Enabled":true,"Start":"2015-06-03T16:55:27.1388252+02:00","End":"2015-06-08T16:55:27.1388252","PublicationId":"19031fa7-2288-4466-94d6-6909d2ed2aa1","MediaId":"91089a91-a4d3-436a-b853-9370972006f3","ItemType":1,"DateStatistics":[{"Date":"2015-06-03T16:55:27.1388252","ScanCount":1138,"ResponseCount":8530},{"Date":"2015-06-04T16:55:27.1388252","ScanCount":4429,"ResponseCount":3556},{"Date":"2015-06-05T16:55:27.1388252","ScanCount":9822,"ResponseCount":121},{"Date":"2015-06-06T16:55:27.1388252","ScanCount":3791,"ResponseCount":3569},{"Date":"2015-06-07T16:55:27.1388252","ScanCount":7275,"ResponseCount":1922},{"Date":"2015-06-08T16:55:27.1388252","ScanCount":7243,"ResponseCount":141}]},{"Id":"47de9aaa-8424-4461-ba72-ae2922777650","CampaignId":"384f5c87-f8c7-43a6-9deb-76340dcf4cd4","CompanyId":"50ede412-4eb0-429b-8d73-a1bfef41ebbc","Name":"Item_7699","Price":1577.0,"Enabled":true,"Start":"2015-06-03T16:55:27.1418263+02:00","End":"2015-06-08T16:55:27.1418263","PublicationId":"19031fa7-2288-4466-94d6-6909d2ed2aa1","MediaId":"91089a91-a4d3-436a-b853-9370972006f3","ItemType":1,"DateStatistics":[{"Date":"2015-06-03T16:55:27.1418263","ScanCount":1138,"ResponseCount":8530},{"Date":"2015-06-04T16:55:27.1418263","ScanCount":4429,"ResponseCount":3556},{"Date":"2015-06-05T16:55:27.1418263","ScanCount":9822,"ResponseCount":121},{"Date":"2015-06-06T16:55:27.1418263","ScanCount":3791,"ResponseCount":3569},{"Date":"2015-06-07T16:55:27.1418263","ScanCount":7275,"ResponseCount":1922},{"Date":"2015-06-08T16:55:27.1418263","ScanCount":7243,"ResponseCount":141}]}],"Companies":{"8df135f4-db42-486c-8d8c-fbbdd561c25e":"Phillips","47d946e3-56db-4bc7-8472-3809eb48506d":"Bauknecht","3ae13b97-7a09-4342-8953-c1d5a55687db":"Capitol","fb017214-dbc1-4b71-8080-4f9b530f4b49":"Bosch","50ede412-4eb0-429b-8d73-a1bfef41ebbc":"LG Corp"},"Campaigns":{"0950aea6-69f3-42c1-99e5-25342c6262ae":"Campagne A","384f5c87-f8c7-43a6-9deb-76340dcf4cd4":"Campagne B"},"Media":[{"Id":"5999703b-a12e-49ad-b054-46875b88ff3a","Name":"Krant","Publications":{"e63f1212-5a21-4546-861c-640007989f08":"Telegraaf","13c3caa6-fcb8-4247-9cf7-8bfe0a0b5056":"Metro","3ddb7f5d-5e28-48ba-8345-5c4e3c7f76c0":"De Volkskrant"}},{"Id":"91089a91-a4d3-436a-b853-9370972006f3","Name":"Televisie","Publications":{"2eac3fd8-b8ed-47b7-8f14-664c3c55425a":"Rtl 4","defab016-d28e-4cf4-b814-0002169cf4cb":"MTV","19031fa7-2288-4466-94d6-6909d2ed2aa1":"Discovery Channel"}},{"Id":"e8610914-52b2-4b38-bc91-bf1bd6d63035","Name":"Radio","Publications":{"3585993c-56b4-4f7d-ac9c-a325eae2e78f":"SkyRadio","15e40597-e479-4a8f-9b25-f39ba90f2c19":"Radio 538","638e1141-f21c-42f1-acd1-652d265c32a1":"Slam FM"}}]};
var result = [];
$.each(a.ReportItems[0], function(k,v){
  var  specificValue = "cf92aefb-b857-49ce-b568-722329377e5d";
  if(v == specificValue){
    result[k]=v;
  }
})
console.log(result);
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48