I am trying to group my JSON object that I get when calling Ajax so I later implement it in my dependable dropdown list. I tried to group the values by CI, but all it did is split each character of all values :/ I tried _.groupBy, for loops ,but nothings seems to work .
The call is now just to test if I would be able to split the files as I want, to work on it later :
$('#my_id').on('change', '#CISelect', function(){
$.ajax({
type:'GET',
url:"{{url_for('CIType')}}",
success:function(data){
console.log(data);
var result = {},
i = 0;
$(data).each(function(key,item){
dataValue = item[i].CI;
if(!result[dataValue]){
result[dataValue] = [];
i++;
}
result[dataValue].push(item[i].CI);
i++;
});
console.log(result);
}
})
my JSON looks like this :
[{"CI":"GDV","Type":"Backup","TypeValue":20613},
{"CI":"GDV","Type":"Carepack","TypeValue":20642},
{"CI":"UNV","Type":"Digitalkamera","TypeValue":335},
{"CI":"UNV","Type":"Dockingstation","TypeValue":250},
{"CI":"PRT","Type":"Fax","TypeValue":325},
{"CI":"MOB","Type":"GSM Gateway","TypeValue":20648}]
what I want is this :
{GDV : [{"Type":"Backup","TypeValue":20613},
{"Type":"Carepack","TypeValue":20642}],
UNV : [{"Type":"Digitalkamera","TypeValue":335},
{"Type":"Dockingstation","TypeValue":250}],
PRT : [{"Type":"Fax","TypeValue":325}},
MOB : {{"Type":"GSM Gateway","TypeValue":20648}]}
Any help would be great.
3
Answers
Result:
You need to make minor adjustment
each iterator needs to be updated
my solution… ( more simple… )
so your code should be :