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.