Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:07:00+00:00 2026-05-28T03:07:00+00:00

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

  • 0

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 select tag) with distinct Groups.

This Groups is an array.

I want that selectbox to have the following values:

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 tried also

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 error appears as: Groups.length is null or not an object

This code appends to select tag the Group values but it appears as duplicates because Groups is an array.

I have to create a new for statement to browse the Groups array ?

Or there is another alternative to avoid nested for statements ?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:07:01+00:00Added an answer on May 28, 2026 at 3:07 am

    2 nested loops could do the job:

    var data = [... your data ...];
    var groups = [];
    $.each(data, function(i, item) {
        $.each(item.Groups, function(j, group) {
            if ($.inArray(group, groups) == -1) {
                groups.push(group);
            }
        });    
    });
    
    // at this stage groups = ["MOD_SW", "MOD_PI", "MOD_GE"]
    

    and if you wanted to directly append options to your dropdown:

    var groups = [];
    $.each(data, function(i, item) {
        $.each(item.Groups, function(j, group) {
            if ($.inArray(group, groups) == -1) {
                groups.push(group);
                $(selectId).append(
                    $('<option/>', {
                        value: group,
                        text: group
                    })
                );
            }
        });    
    });
    

    UPDATE:

    And to make this more efficient you could define a static dtstinct method:

    $.extend({
        distinct : function(arr) {
           var result = [];
           $.each(arr, function(index, value) {
               if ($.inArray(value, result) == -1) {
                   result.push(value);
               }
           });
           return result;
        }
    });
    

    and then use the .map method:

    var data = [... your data ...];
    var groups = $.distinct($(data).map(function() {
        return this.Groups;    
    }));
    $.each(groups, function(index, group) {
        $(selectId).append(
            $('<option/>', {
                value: group,
                text: group
            })
        );
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a json output array like this { data: [ { name: Ben
I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19
I have a json object that looks something like this: Array ( [algorithm] =>
i have this JSON: var projects_array = new Array( {name:myName1, id:myid1, index:1}, {name:myName2, id:myid2,
I have some troubles mapping a JSON Array to RestKit. This is what the
I have a JSON array that looks like this: ['Monkey','Cheetah','Elephant','Lizard','Spider'] I also have a
I have json array like this one: [{code : A1, desc : desc1},{code :
[I have some code to create a JSON array. In this code I am
I have a a JSON-encoded Array which looks like this (note: this is in
I have a JSON string array of objects like this. [{id:4,rank:adm,title:title 1}, {id:2,rank:mod,title:title 2},

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.