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

  • SEARCH
  • Home
  • 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 7929367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:03:04+00:00 2026-06-03T20:03:04+00:00

I have an array of objects with different properties and I need to filter

  • 0

I have an array of objects with different properties and I need to filter the array in a way that a specific property is not duplicated.

For example:

var array:Array = [{foo:"a1", bar:"b1", baz:"c1"},
                   {foo:"a2", bar:"b2", baz:"c2"},
                   {foo:"a3", bar:"b1", baz:"c3"},
                   {foo:"a1", bar:"b4", baz:"c2"},
                   {foo:"a0", bar:"b3", baz:"c1"}];

Now suppose I want to filter the objects on the property baz. What is the most efficient way of filtering the array, so that no two elements have the same value for baz after the operation?

In my example, the result should only contain:

var result:Array = [{foo:"a1", bar:"b1", baz:"c1"},
                    {foo:"a2", bar:"b2", baz:"c2"},
                    {foo:"a3", bar:"b1", baz:"c3"}]

since the other objects would have duplicate entries for the baz property.

The order of the result array is not important, neither is which object of those with identical values for baz makes it into the result array.


Update:

The object array is used as a dataprovider to populate a s:Datagrid with information about chatrooms. The objects in the array carry related information (like the room’s ID on the server and some other config settings).

The baz property I used in my example is actually the ID of the language the chat room is configured to use and I want to create a s:DropDownList with which I can filter the Datagrid for individual languages (e.g. show all rooms that use "German").

It is very likely to have many objects with the same language ID, but I only want each language Id to show up once in the DropDownList.

I need to extract that information from the Datagrids's dataprovider (the source array) and cannot retrieve my languages directly since the DropDownList is part of a generic DatagridHeaderRenderer that is used in many different Datagrids with different data.

  • 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-06-03T20:03:08+00:00Added an answer on June 3, 2026 at 8:03 pm

    On the surface of it looks like it should work. Using Array.filter is usually about twice the time of doing the same thing in a loop.

    I’d argue that’ Dom’s removeDupes function doesn’t do exactly what’s required, although it might be a more generic approach (if, for example, the === isn’t a good comparison function, then this gives you a way of extending it.) But using hasOwnPropery is a big no-no. You should never touch it – that function only exists for ES compatibility. It is evil otherwise – both a potential security hole (as it is defined on Object.prototype and thus is easy to override for the foreign code) and is slow (for the same reason – the lookup of the functions defined on prototype is slower then those defined in a class).

        public function Test()
        {
            super();
            var array:Array = [{foo:"a1", bar:"b1", baz:"c1"},
                {foo:"a2", bar:"b2", baz:"c2"},
                {foo:"a3", bar:"b1", baz:"c3"},
                {foo:"a1", bar:"b4", baz:"c2"},
                {foo:"a0", bar:"b3", baz:"c1"}];
            this.removeDuplicates(array, "baz").map(this.objectTracer);
            // { foo : a3, baz : c3, bar : b1 }
            // { foo : a1, baz : c2, bar : b4 }
            // { foo : a0, baz : c1, bar : b3 }
        }
    
        private function objectTracer(object:Object, index:int, all:Array):void
        {
            var result:String = "";
            for (var p:String in object)
                result += ", " + p + " : " + object[p];
            if (result) result = result.substr(2);
            trace("{ " + result + " }");
        }
    
        private function removeDuplicates(array:Array, on:String):Array
        {
            var result:Array = array.concat();
            // note that since we use `Dictionary' the 
            // the comparison between objects is the same as `==='
            // if all values are strings, you can use `Object' to
            // save some space.
            var hash:Dictionary = new Dictionary();
            var temp:Object;
    
            for (var i:int, j:int = result.length - 1; j >= i; j--)
            {
                temp = result[j][on];
                if (temp in hash)
                {
                    result[j] = result[i];
                    j++; i++;
                }
                else hash[temp] = true;
            }
            // note that we could `shift()` until we get to `i'
            // but when we do it, we actually reallocate the array every time
            // so `slice()' must, in theory, be more efficient
            return result.slice(i);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that can store different types of objects. When I retrieve
I have an array of objects. During the loop I append different properties to
I have an array of objects that have QTTime structs as attributes. The objects
According to MDN documentation for JSON.stringify : Properties of non-array objects are not guaranteed
I have an array of different type objects and I use a BinaryWriter to
I have an array of different objects with different data types I want to
I have a collection of objects and I need to log their properties to
I have an array of objects, and want the objects to be able to
I have an array of objects in MATLAB and I've called their constructors in
I have an array of objects[] but I want to convert them to an

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.