How to search into an array of objects?

Ask a Question related to Macromedia Flex General Discussion, Design and Development.

  1. #1

    Default How to search into an array of objects?

    Hi,

    Is there a way to find an item into a array of object without parse each
    object?

    Ex:
    var : myArray : Array = new Array(
    {label : "cup", ref : "345TR3"},
    {label : "glass", ref : "987TR3"},
    {label : "spool", ref : "123XX3"},
    ...)

    How to search for ref "987TR3" and get array index (=1) and label (=glass or
    = myArray[1].label) ?

    Thank in advance,

    FredFlex

    FredFlex Guest

  2. Similar Questions and Discussions

    1. Array out of Objects
      Back in the old days of Flash, I was able to walk through all of the objects on the stage, and filter them by the type of object. I could then...
    2. How to represent an array of objects?
      If I have the following class declaration and then need to be able to provide a way to access a collection of PartyRecords, how would I accomplish...
    3. Custom objects in an array
      I have tried to store instances of a custom class in an array. When I retrieve the object actionScript seems to have forgotten what type of object...
    4. array of objects
      I currently have a number of buttons, which have image icons and when clicked, will display th associated image in a large viewing fram I would...
    5. What is this objects name in the array?!
      I have this code sniplet.. function dragalong() { this.startDrag(); } function dropper() { this.stopDrag(); }
  3. #2

    Default Re: How to search into an array of objects?

    The ordinary old array does not.

    You could use what is called an "associative array", which is simply an
    mx:Object. This use is very similar to a "hashmap" in other languages.

    To add an element:
    oAssocArray.anyString = AnyObjectOrStringOr anything;

    To retrieve an element:
    var myValue:WhateverType = oAssocArray.anyString;

    You can also look at the ArrayCollection class. I think the "contains" method
    provides random access.

    Tracy


    ntsiii Guest

  4. #3

    Default Re: How to search into an array of objects?

    Associative arrays, which are sometimes called hashes or maps, use keys instead
    of a numeric index to organize stored values. Each key in an associative array
    is a unique string that is used to access a stored value.

    There are two ways to create associative arrays in ActionScript 3.0. The first
    way is to use the Object constructor, which has the key advantage of allowing
    you to initialize your array with an object literal. An instance of the Object
    class, also called a generic object, is functionally identical to an
    associative array. Each property name of the generic object serves as the key
    that provides access to a stored value.

    The following example creates an associative array named monitorInfo, using an
    object literal to initialize the array with two key and value pairs:

    var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
    trace (monitorInfo["type"], monitorInfo["resolution"]);
    // output: Flat Panel 1600 x 1200

    If you do not need to initialize the array at declaration time, you can use
    the Object constructor to create the array, as follows:

    var monitorInfo:Object = new Object();

    After the array is created using either an object literal or the Object class
    constructor, you can add new values to the array using either the bracket
    operator ([]) or the dot operator (.). The following example adds two new
    values to monitorArray:

    monitorInfo["aspect ratio"] = "16:10"; // bad form, do not use spaces
    monitorInfo.colors = "16.7 million";
    trace (monitorInfo["aspect ratio"], monitorInfo.colors);
    // output: 16:10 16.7 million

    Note that the key named aspect ratio contains a space character. This is
    possible with the bracket operator, but generates an error if attempted with
    the dot operator. Using spaces in your key names is not recommended.

    The second way to create an associative array is to use the Array constructor
    and then use either the bracket operator ([]) or the dot operator (.) to add
    key and value pairs to the array. If you declare your associative array to be
    of type Array, you cannot use an object literal to initialize the array. The
    following example creates an associative array named monitorInfo using the
    Array constructor and adds a key called type and a key called resolution, along
    with their values:

    var monitorInfo:Array = new Array();
    monitorInfo["type"] = "Flat Panel";
    monitorInfo["resolution"] = "1600 x 1200";
    trace (monitorInfo["type"], monitorInfo["resolution"]);
    // output: Flat Panel 1600 x 1200

    There is no advantage in using the Array constructor to create an associative
    array. You cannot use the Array.length property or any of the methods of the
    Array class with associative arrays, even if you use the Array constructor or
    the Array data type. The use of the Array constructor is best left for the
    creation of indexed arrays.

    poonamsheth Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139