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 4615592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:51:43+00:00 2026-05-22T01:51:43+00:00

I have an Array of Objects which I want to filter depending on some

  • 0

I have an Array of Objects which I want to filter depending on some search Strings. I want to create a new array from my original array which will only contain Objects which have properties equal to the Strings in the search:

    var _array =  new Array();
    _array.push({name:"Ben",Title:"Mr",location:"UK"});
    _array.push({name:"Brian",Title:"Mr",location:"USA"});
    _array.push({name:"Ben",Title:"Mr",location:"USA"});

    var searchQuery:Array = new Array();
    searchQuery.push("Ben");
    searchQuery.push("Mr");

I want the new Array to contain the first and the last Object as they both contain the Strings “Ben” and “Mr”.

Can I use the Array.filter to achieve this?

Any help appreciated.

  • 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-22T01:51:44+00:00Added an answer on May 22, 2026 at 1:51 am

    Ah nothing quite like a good old collections problem 🙂

    Although JiminP’s answer is indeed correct; it suffers from a couple of performance concerns; the biggest of which being that closures in AS3 are slow, so if you are searching over a large Array the operation could run slowly.

    The following function isn’t quite as clean, but will yeild better performance over a large Array.

    var _array : Array = [];
    _array.push({name:"Ben", Title:"Mr", location:"UK"});
    _array.push({name:"Brian", Title:"Mr", location:"USA"});
    _array.push({name:"Ben", Title:"Mr", location:"USA"});
    
    // I presumed you would want a little bit more control over the search matching; by
    // using a Map you can ensure that no-one with the (somewhat unlikley) name of "Mr"
    // gets matched by mistake.
    var searchQueryMap : Dictionary = new Dictionary();
    searchQueryMap["name"] = "Ben";
    searchQueryMap["Title"] = "Mr";
    
    const results : Array = [];
    
    // Loop over earch objectin the 'haystack' that we wish to search.
    for each (var object : Object in _array) 
    {
        // This variable is used to break out of the loop if the current object doesn't match the
        // searchQueryMap; this gets reset to true for each loop of the supplied array.
        var match : Boolean = true;
    
        // Loop over each key (property) in the searchQueryMap.
        for (var key : * in searchQueryMap) 
        {
            if (searchQueryMap[key] !== object[key]) 
            {
                // No match, we can break out of looping over the searchQueryMap here.
                match = false;
                break;          
            }
        }
    
        // Check to see if we still have a positive match; if we do, push it onto the results Array.
        if (match) {
            results.push(object);   
        }
    }
    
    // Debug the results.
    trace("Matches:");
    for each (var result : Object in results)
    {
        for (var prop : * in result) {
            trace("\t" + prop + " => " + result[prop]);
        }
        trace("---");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects and I want to find which element in
I have an array which contains objects some may be same and some are
I have an array of objects which I want to serialize as XML. These
I have an array of objects which created from a custom class. The custom
I have an array of objects which I want to sort based on one
I have an array of objects, some of which respond to :description, and I
I have an array which I want to filter out certain keys. Let's say
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have an array of objects which populate a UITableView . When a user
I have an array of FileReference objects which have several listeners attached to each

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.