I have a JSON array like this
[{"Email":" someone@some.com ","Name":"ACO","Groups":["MOD_SW","MOD_PI","MOD_GE"],"Id":63,"Url":"aco"}, {"Email":" someone@some.com ","Name":"Agpo","Groups":["MOD_PI"],"Id":22,"Url":"agpo"}, {"Email":" someone@some.com ","Name":"Akatherm","Groups":["MOD_SW"],"Id":64,"Url":"akatherm"}, {"Email":" someone@some.com ","Name":"Albrand","Groups":["MOD_PI,"MOD_GE"],"Id":23,"Url":"albrand"}]
I want to create a new array (for the selected tag) with excellent Groups .
This Groups is an array.
I want this selectbox to have the following meanings:
MOD_SW MOD_PI MOD_GE
My JS:
UpdateSelectMenu: function (selectId, data) { $(selectId).empty(); $(selectId).html("<option value='all' selected='selected'>All groups</option>"); var array_unique_values = []; for (var i = 0; i < data.Groups.length; i++) { for (var j = i+1; j < data.Groups.length; j++) { if (data.Groups[i] === data.Groups[j]) { j = ++i; } } array_unique_values.push(data.Groups[i]); } array_unique_values = array_unique_values.sort(); $.each(array_unique_values, function (k, v) { $(selectId).append("<option value='" + v + "'>" + v + "</option>"); }); }
I also tried
for (var i = 0; i < data.length; i++) { //browse whole data for (var j = 0; j < data[i].Groups.length; j++) { //browse Groups array for (var k = j + 1; j < data[i].Groups.length; k++) { if (data[i].Groups[j] === data[i].Groups[k]) { continue; } } array_unique_values.push(data[i].Groups[j]); } }
But the error appears as: Groups.length is null or not an object
This code adds select tags to group values, but it appears as duplicates, because Groups is an array.
Do I need to create a new for statement to view the Groups array?
Or is there another alternative to avoid nested for statements?
thanks