1

I'm struggeling to push some values into an array. I want to push the values into the array when the key or possible id match.

I tried several things but it keeps returning an empty array.

What I tried:

 function setAttributes(matched){
   var url = 'url-to-json-file';
   var variant = [];
   $.getJSON(url, function(data){
     var match = $.inArray(matched, data.variants) != -1
     if(match){
       variant.push({
         id: variant.id, //etc etc
       });
     }               
   });
   return variant
 }

 $(function(){ 
   var chkd = $('#order input:checked').attr('id') // example 161190021
   setAttributes(chkd)
 });

My JSON looks like this (heavily minified, file works):

{
  "variants":{
    "161190021":{"id":161190021},
    "161190024":{"id":161190024},
    "161190027":{"id":161190027}
  }
}

To be complete the HTML:

   <table id="order" .... etc
     <tr class="161190021">
        <td>
          <input type="radio" checked="checked" value="161190021" name="variant" id="161190021">
          <label for="161190021">S</label>
        </td>
      </tr>  

      <tr class="161190024">
        <td>
          <input type="radio" value="161190024" name="variant" id="161190024">
          <label for="161190024">M</label>
        </td>
      </tr>  

      <tr class="161190027">
        <td>
          <input type="radio" value="161190027" name="variant" id="161190027">
          <label for="161190027">Maat: L (35713)</label>
        </td>
      </tr>  
      </table>    

So basically I want to test if variable chkd is present in data.variants. And if that's the case I want to push the values from that matched variant into an array called variant.

I tried things like:

var match = $.inArray(matched, data.variants) != -1 
var match = $.grep(data.variants, function(e){ return e.id == matched; });

But it seems like none of them work, they return an empty array variant! What am I doing wrong?

Any help greatly appreciated.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Meules
  • 1,349
  • 4
  • 24
  • 71
  • `variants` is not an array. None of your json is an array. It's all objects. – Taplar Nov 08 '18 at 14:42
  • Also `if(match)` has a logic flaw for if the match is 0, which is a valid index. – Taplar Nov 08 '18 at 14:42
  • And `return variant` will not work because it runs before the $.getJSON call has finished (because AJAX calls are _asynchronous_) – ADyson Nov 08 '18 at 14:43
  • 1
    ^^^ which is kind of pointless as the caller doesn't capture and use the return – Taplar Nov 08 '18 at 14:44
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Taplar Nov 08 '18 at 14:44
  • It's unclear where you're executing the lines shown like `var match = ` etc but if they're working with the value returned from calling the setAttributes method then they won't work for exactly the reason in my comment above. See the duplciate for detail, but basically any code which relies on the JSON needs to be executed within the callback in the $.getJSON (or in a function triggered from the callback). Of course if you make setAttributes return the promise returned by $getJSON, then code external to set attributes can attach its own ".done()" handler to receive the data and process it – ADyson Nov 08 '18 at 14:48
  • @all: So basically best would be to create an array first. Then in a new function test the array for matching values? – Meules Nov 08 '18 at 14:57
  • It doesn't really matter so much...the key point is that you have to wait for the AJAX request to complete before you do any comparing of values – ADyson Nov 08 '18 at 22:35

0 Answers0